简体   繁体   English

Djoser / Django Rest Framework /用户注册:如何存储非必需的用户字段

[英]Djoser / Django Rest Framework / User Registration: How to store non required user fields

I am using Djoser and Django Rest Framework for user registration with a custom user model. 我正在使用DjoserDjango Rest Framework通过自定义用户模型进行用户注册。 When I try to add NON REQUIRED fields to my custom user models and pass them (as **kwargs) , these additional fields do not get saved. 当我尝试将“不需要”字段添加到我的自定义用户模型并将其传递(作为** kwargs)时,不会保存这些其他字段。

Is this by design or is there a trick to saving these? 这是设计使然还是保存这些窍门?

    class UserManager(BaseUserManager):

        def create_user(self, email, phone, password, **kwargs):
            user = self.model(
                email=email,
                phone=phone,
                **kwargs
            )

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

        def create_superuser(self, email, phone, password, **kwargs):
            user = self.model(
                email=email,
                phone=phone,
                is_staff=True,
                is_active=True,
                **kwargs
            )

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

    class User(AbstractBaseUser):
        userId = models.AutoField(primary_key=True)
        email = models.CharField(unique=True, max_length=45, null=False)
        phone = models.CharField(max_length=15, unique=True, null=False)
        password = models.CharField(max_length=255, blank=True, null=True)
        is_active = models.IntegerField(blank=True, null=True)

        class Meta:
            managed = False
            db_table = 'User'

        objects = UserManager()

        USERNAME_FIELD = 'email'
        REQUIRED_FIELDS = ['phone']

        def get_full_name(self):
            return self.email
        def get_short_name(self):
            return self.email
        def natural_key(self):
            return self.email
        def __str__(self):
            return self.email

Looks like this is a known Djoser issue and there is a workaround as mentioned here; 看起来这是一个已知的Djoser问题,这里有一个解决方法。 github.com/sunscrapers/djoser/issues/249 github.com/sunscrapers/djoser/issues/249

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM