Answer the question
In order to leave comments, you need to log in
How to limit the number of objects created by a user per day?
Hello, in my project, I want to implement some restrictions for users, which is that the user cannot add more than a certain number of objects per day. It is also necessary to make sure that, in addition to the daily limit, there is a time interval of a certain number of hours between the creation of objects.
My guess is that you can add a numeric attribute to the user model that will decrease as new objects are created, but you also need to have this counter automatically updated every 24 hours.
Another point is that if a user publishes one object per day, then he will have two more, and from this it follows that when the counter is resumed the next day, he will already have 5 entries. This point also needs to be taken into account.
Help, please, with ideas of implementation.
Answer the question
In order to leave comments, you need to log in
My guess is that you can add a numeric attribute to the user model that will decrease as new objects are created, but you also need to have this counter automatically updated every 24 hours.
Another point is that if a user publishes one object per day, then he will have two more, and from this it follows that when the counter is resumed the next day, he will already have 5 entries. This point also needs to be taken into account.
if SomeModel.objects.filter(
author=request.user,
timestamp__gte=timezone.now()-datetime.timedelta(days=1),
).count() < LIMIT:
# allow create
else:
# decline create
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question