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
DateFieldinstead ofDateTimeField, 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: