The username field name is not valid for the model

I am trying to use the serializers supplied by rest-auth in GET (* with headers) user data from a specific endpoint / rest- aut / user /

(* with headers (Content-Type: application / json Authorization: token 1a5472b2af03fc0e9de31fc0fc6dd81583087523))

I get the following trace: https://dpaste.de/oYay#L

I defined the user model of the user (using email, not the username) as such:

class UserManager(BaseUserManager): def create_user(self, email, password, **kwargs): user = self.model( # lower-cases the host name of the email address # (everything right of the @) to avoid case clashes email=self.normalize_email(email), is_active=True, **kwargs ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, password, **kwargs): user = self.model( email=email, is_staff=True, is_superuser=True, is_active=True, **kwargs ) user.set_password(password) user.save(using=self._db) return user class MyUser(AbstractBaseUser, PermissionsMixin): USERNAME_FIELD = 'email' email = models.EmailField(unique=True) 

Settings:

 AUTH_USER_MODEL = 'users.MyUser' ACCOUNT_USER_MODEL_USERNAME_FIELD = None # Config to make the registration email only ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_UNIQUE_EMAIL = True ACCOUNT_EMAIL_VERIFICATION = 'optional' ACCOUNT_USERNAME_REQUIRED = False ACCOUNT_AUTHENTICATION_METHOD = 'email' EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' 

You do not know how to fix this error .. so that it matches the rest-auth serializers.

+5
source share
1 answer

In django-rest-auth they have a default defaul serializer for user models

t

 USER_DETAILS_SERIALIZER = 'rest_auth.views.UserDetailsView' 

And here they serialize djang.contrib.auth.User

In your case, you are using the user model of the user, and you do not have the usernam field in your models, so it gives an error when trying to serialize the user name of the field. Therefore, you need to write a serializer for your user model and add a path to your settings:

Example:

 class CustomUserDetailsSerializer(serializers.ModelSerializer): class Meta: model = MyUser fields = ('email',) read_only_fields = ('email',) 

In settings.py

 USER_DETAILS_SERIALIZER = CustomUserDetailsSerializer 
+8
source

Source: https://habr.com/ru/post/1237233/


All Articles