How to make email field unique in model User from contrib.auth in Django

后端 未结 19 1905
夕颜
夕颜 2020-11-27 11:47

I need to patch the standard User model of contrib.auth by ensuring the email field entry is unique:

User._meta.fields[4].unique = True
<         


        
相关标签:
19条回答
  • 2020-11-27 12:20

    I think that the correct answer would assure that uniqueness check was placed inside the database (and not on the django side). Because due to timing and race conditions you might end with duplicate emails in the database despite having for example pre_save that does proper checks.

    If you really need this badly I guess you might try following approach:

    1. Copy User model to your own app, and change field email to be unique.
    2. Register this user model in the admin app (using admin class from django.contrib.auth.admin)
    3. Create your own authentication backend that uses your model instead of django one.
    0 讨论(0)
提交回复
热议问题