雖然我沒有時間來測試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