开发者

Use Django's get_or_create to create a model for each day

I have created a model with a field:

date = models.DateTimeField(auto_now_add=True)

I want to create a new record for every day that this object is accessed. I assume I should use Django's get_or_create function to get the object that already exists, or create a new object with today's date. But how do I have it 开发者_高级运维check whether the date = today's month/day/year?


You should use DateField instead of DateTimeField, and if you need to create something more complex (like one new field per hour), you should try to create your own field or use filters.

If you want to use filters, just do something like this:

get_or_create(Model, date__year=datetime.now().year, [...])

You can also create a cron task that create a field each day.

These articles can ask to your question:

  • http://docs.djangoproject.com/en/1.2/ref/models/fields/#datefield
  • docs.djangoproject.com/en/1.2/howto/custom-model-fields/
  • docs.djangoproject.com/en/1.2/topics/db/queries/#retrieving-specific-objects-with-filters
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜