I have a DurationField (time_open_for) in my model as well as a BooleanField (is_open) which keeps track of whether an instance of this model is open or not (defaults to true). The model also has a date_created (DateTimeField). What I wish to happen is for when the time_open_for runs out for the is_open to set itself to False. I know I can do this using logic inside views: something like
if datetime.datetime.now(tz=pytz.UTC) > (date_created + time_open_for): model_name.is_open = False model_name.save()
I have been doign this and it works but the problem i’m having is that the view function actually needs to get called in order to close a model (set
is_open=False). This means that even though sufficient time may have passed for my model to close it may remain open since a user may not have clicked on something which would have closed it. I was wondering if there might perhaps be some way of enforcing the database itself to periodically call the function independent of user activity so as to check the time constraints and if necessary inactivate those instances whos
open_until time has elapsed?
Any help would be kindly appreciated. Perhaps I’ve got the wrong idea of how to approach/use DurationFields in models.