Multiple USERNAME_FIELD in django user model

后端 未结 5 1883
清歌不尽
清歌不尽 2020-12-16 02:22

My custom user model:

class MyUser(AbstractBaseUser):
    username = models.CharField(unique=True,max_length=30)
    email = models.EmailField(unique=True,ma         


        
5条回答
  •  Happy的楠姐
    2020-12-16 02:58

    No, you cannot have more than one field defined in USERNAME_FIELD.

    One option would be to write your own custom login to check for both fields yourself. https://docs.djangoproject.com/en/1.8/topics/auth/customizing/

    i.e. change the backend to your own. AUTHENTICATION_BACKENDS then write an authenticate method and check the username on both fields in the DB.

    PS you may want to use unique_together on your model so you don't run into problems.

    Another option would be to use the actual field username to store both string and email.

提交回复
热议问题