(对不起我的英语不好)
只有一个问题,是否有任何方法可以限制 Django 应用程序中可以创建的用户数量?
我搜索了很多地方,只找到了这个,但我在仓库中看到最后一次更新是 3 年前https://github.com/1stvamp/django-limit-users https://github.com/1stvamp/django-limit-users
我不知道 django 的核心中是否存在任何方式,或者我是否必须覆盖某些东西!
非常感谢!
虽然我没有时间测试https://github.com/1stvamp/django-limit-users https://github.com/1stvamp/django-limit-users针对新的 Django,它使用 django 的信号朝着正确的方向发展:https://docs.djangoproject.com/en/dev/ref/signals/ https://docs.djangoproject.com/en/dev/ref/signals/
例如,你可以写一个pre_save
or a post_save
处理程序并将其连接到保存用户模型之前/之后发出的信号。
一个简单的post_save
处理程序可能如下所示:
def user_post_save(sender, instance, created, **kwargs):
if created and sender.objects.count() > MY_LIMIT:
instance.is_active = False
instance.save()
一个简单的pre_save
处理程序看起来像:
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
而不是最后一行pre_save
处理程序,您还可以引发异常以确保用户甚至没有保存到数据库中。
另一种选择是将其与自定义用户模型结合起来,而不是is_active
你可以用over_limit
或者任何你想要的。您链接的存储库是通过单独的DisabledUser
model.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)