Django - limiting the number of users
(sorry for my English)
Just a question, is there any way to limit the number of users that can be created in a Django application?
I search in many places and I only find this, but I can see in the repo that the last update was 3 years ago https://github.com/1stvamp/django-limit-users
I don't know if any path exists in django core or if I need to override something!
Many thanks!
Until I have time to check https://github.com/1stvamp/django-limit-users for the new Django, it goes in the right direction using django signals: https://docs.djangoproject.com/en/dev/ref / signals /
So, for example, you can write a handler pre_save
or post_save
and hook it up to signals emitted before / after saving your user model.
A simple post_save
handler might look like this:
def user_post_save(sender, instance, created, **kwargs):
if created and sender.objects.count() > MY_LIMIT:
instance.is_active = False
instance.save()
A simple pre_save
handler would look like this:
def user_pre_save(sender, instance, **kwargs):
if instance.id is None and sender.objects.count() > MY_LIMIT:
instance.is_active = False # Make sure the user isn't active
Instead of the last line in the handler, pre_save
you can also throw an exception to make sure the User is not even saved to the DB.
Another option would be to combine this with a custom user model, so is_active
you could use over_limit
or whatever instead . The repo you linked achieves this with a separate model DisabledUser
.