虽然我没有时间来测试https://github.com/1stvamp/django-limit-users对一个新的Django,它使用Django的信号进入在正确的方向到保存用户模型之前/之后发出的信号。
一个简单的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
模型实现的。
现在已经很晚了,但是明天我会尝试django-limit-users,如果不工作使它成为我自己的。谢谢 !!! – GAMES