Skip to content
Advertisement

Return the number of times my django model object was retrieved today

With a model like below, I want to return the number of times an object was retrieved today

JavaScript

The Stamping is another model with created_at and updated_at

Below is the example of what I want to achieve

JavaScript

Advertisement

Answer

You can simply add a total_count and a daily_count column, then reset the daily_count every day at midnight. For example:

JavaScript

Then increase both fields from your get_or_create() handler.

You can then execute a piece of code every midnight that resets the daily_count for all Watched instances. I recommend you to write a custom django-admin command and run it via a cron job or similar:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement