如何使用MongoEngine向EmbeddedDocument添加更多项目?

时间:2012-06-03 15:17:52

标签: python django mongodb mongoengine

我正在尝试检查是否已有一个包含当前日期的项目,如果没有,我会添加它。下面的代码没有提示任何错误,但它没有保存任何东西到MongoDB

if len(site.DaylyLog) != 0:
    print len(site.DaylyLog)
    print site.DaylyLog[-1]
    current_date_daylylog = site.DaylyLog[-1]['aggregate_date']
else:
    current_date_daylylog = ''

if current_date_daylylog == datetime.date.today():
    #update document
    print current_date_daylylog
    print "calculating averages"

    site.DaylyLog[-1].average_response_time = (site.DaylyLog[-1].average_response_time+record_uptime.response_time)/2
    site.DaylyLog[-1].average_uptime = (site.DaylyLog[-1].average_uptime+100)/2

    site.save()
else:
    print current_date_daylylog
    print "adding new uptime"

    print site.url
    aggregate_uptime = AggregatedUptime()
    aggregate_uptime.average_response_time = record_uptime.response_time
    aggregate_uptime.average_uptime = 100
    aggregate_uptime.total_downtime = 0
    aggregate_uptime.aggregate_date = datetime.date.today()

    print aggregate_uptime.aggregate_date
    print aggregate_uptime.average_response_time

    site.DaylyLog.append(aggregate_uptime)
    site.save()

    print site.DaylyLog[-1].aggregate_date

1 个答案:

答案 0 :(得分:0)

正如我在上面的评论中提到的那样。我发布的代码还可以,但它正在重新修改我在模型中发生的变化。这只是一个新手问题。 : - )

旧模型是

class Checkr(Document):
 name = StringField()
 slug = SlugField()
 url = URLField()
 frequency = IntField()
 owner = IntField()
 start_date = DateTimeField()
 end_date = DateTimeField()
 current_response_time = FloatField()
 current_response_code = IntField()
 hourly_log = ListField(EmbeddedDocumentField(AggregatedUptime))
 dayly_log = ListField(EmbeddedDocumentField(AggregatedUptime))
 weekly_log = ListField(EmbeddedDocumentField(AggregatedUptime))

什么时候应该

class Checkr(Document):
 name = StringField()
 slug = SlugField()
 url = URLField()
 frequency = IntField()
 owner = IntField()
 start_date = DateTimeField()
 end_date = DateTimeField()
 current_response_time = FloatField()
 current_response_code = IntField()
 HourlyLog = ListField(EmbeddedDocumentField(AggregatedUptime))
 DaylyLog = ListField(EmbeddedDocumentField(AggregatedUptime))
 WeeklyLog = ListField(EmbeddedDocumentField(AggregatedUptime))