0

我正在尝试检查是否已经存在具有当前日期的项目,如果没有,我将添加它。下面的代码没有提示任何错误,但它没有将任何内容保存到 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
4

1 回答 1

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))
于 2012-06-03T20:49:51.947 回答