M
M
Mikkkch2020-12-08 17:28:26
Django
Mikkkch, 2020-12-08 17:28:26

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

1 answer(s)
V
Vladimir Kuts, 2020-12-08
@Mikkkch

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.

Too complicated... Better something like this:
if SomeModel.objects.filter(
    author=request.user,
    timestamp__gte=timezone.now()-datetime.timedelta(days=1),
).count() < LIMIT:
     # allow create 
else:
    # decline create

Just store the author who created the model and the timestamp in the model object. And just see how many objects he created in a certain period.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question