简体   繁体   中英

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM