2017-08-10 35 views
1

所以我創建了我自己的用戶模型,按照文檔建議的方式對AbstractBaseUser進行子分類。這裏的目標是使用一個名爲mob_phone的新字段作爲註冊和登錄的標識字段。Django自定義用戶 - 不使用用戶名 - 用戶名唯一約束失敗

它對第一個用戶來說很有魅力。它將username字段設置爲nothing - blank.But當我註冊第二個用戶時,我得到一個「UNIQUE約束失敗:user_account_customuser.username」。

我基本上想完全消除username字段。我怎樣才能做到這一點?

我已經嘗試了很多我在別處找到的建議,但無濟於事。我基本上需要找到一種方法,使用戶名字段不唯一或完全刪除它。

乾杯!

院長

models.py

from django.contrib.auth.models import AbstractUser, BaseUserManager 


class MyUserManager(BaseUserManager): 
    def create_user(self, mob_phone, email, password=None): 
     """ 
     Creates and saves a User with the given mobile number and password. 
     """ 
     if not mob_phone: 
      raise ValueError('Users must mobile phone number') 

     user = self.model(
      mob_phone=mob_phone, 
      email=email 
     ) 

     user.set_password(password) 
     user.save(using=self._db) 
     return user 

    def create_superuser(self, mob_phone, email, password): 
     """ 
     Creates and saves a superuser with the given email, date of 
     birth and password. 
     """ 
     user = self.create_user(
      mob_phone=mob_phone, 
      email=email, 
      password=password 
     ) 
     user.is_admin = True 
     user.save(using=self._db) 
     return user 


class CustomUser(AbstractUser): 
    mob_phone = models.CharField(blank=False, max_length=10, unique=True) 
    is_admin = models.BooleanField(default=False) 
    objects = MyUserManager() 

    # override username field as indentifier field 
    USERNAME_FIELD = 'mob_phone' 
    EMAIL_FIELD = 'email' 

    def get_full_name(self): 
     return self.mob_phone 

    def get_short_name(self): 
     return self.mob_phone 

    def __str__(self):    # __unicode__ on Python 2 
     return self.mob_phone 

    def has_perm(self, perm, obj=None): 
     "Does the user have a specific permission?" 
     # Simplest possible answer: Yes, always 
     return True 

    def has_module_perms(self, app_label): 
     "Does the user have permissions to view the app `app_label`?" 
     # Simplest possible answer: Yes, always 
     return True 

    @property 
    def is_staff(self): 
     "Is the user a member of staff?" 
     # Simplest possible answer: All admins are staff 
     return self.is_admin 

堆棧跟蹤:

Traceback (most recent call last): File "manage.py", line 22, in execute_from_command_line(sys.argv) File "/home/dean/.local/lib/python3.5/site-packages/django/core/management/init.py", line 363, in execute_from_command_line utility.execute() File "/home/dean/.local/lib/python3.5/site-packages/django/core/management/init.py", line 355, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/dean/.local/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv self.execute(*args, **cmd_options) File "/home/dean/.local/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute return super(Command, self).execute(*args, **options) File "/home/dean/.local/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute output = self.handle(*args, **options) File "/home/dean/.local/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 183, in handle self.UserModel._default_manager.db_manager(database).create_superuser(**user_data) File "/home/dean/Development/UrbanFox/UrbanFox/user_account/models.py", line 43, in create_superuser password=password File "/home/dean/Development/UrbanFox/UrbanFox/user_account/models.py", line 32, in create_user user.save(using=self._db) File "/home/dean/.local/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 80, in save super(AbstractBaseUser, self).save(*args, **kwargs) File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py", line 807, in save force_update=force_update, update_fields=update_fields) File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py", line 837, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py", line 923, in _save_table result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py", line 962, in _do_insert using=using, raw=raw) File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/query.py", line 1076, in _insert return query.get_compiler(using=using).execute_sql(return_id) File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1107, in execute_sql cursor.execute(sql, params) File "/home/dean/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/home/dean/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) File "/home/dean/.local/lib/python3.5/site-packages/django/db/utils.py", line 94, in exit six.reraise(dj_exc_type, dj_exc_value, traceback) File "/home/dean/.local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise raise value.with_traceback(tb) File "/home/dean/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) File "/home/dean/.local/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: UNIQUE constraint failed: user_account_customuser.username

回答

0

好吧,我是個白癡。這張貼在明顯的解決方案後,從字面上秒發生在我身上:

username = models.CharField(max_length=40, unique=False, default='') 

只是覆蓋用戶名字段並使其不唯一。

橡皮鴨理論在行動....

相關問題