my user model:
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
national_code = models.CharField(
_('national code'), max_length=10, blank=True, null=True
)
phone_number = models.CharField(
_('phone number'), max_length=13, blank=True, null=True
)
is_active = models.BooleanField(_('active'), default=True)
is_staff = models.BooleanField(_('staff'), default=False)
avatar = models.ImageField(
_('avatar'), upload_to='avatars', default='avatars/defaults.png'
)
age = models.PositiveSmallIntegerField(_('age'), blank=True, null=True)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
@property
def get_full_name(self):
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
@property
def get_short_name(self):
return self.first_name
def email_user(self, subject, message, from_email=None, **kwargs):
send_mail(subject, message, from_email, [self.email], **kwargs)
user model manager:
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_staff', True)
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
user serializer:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (
'id',
'email',
'first_name',
'last_name',
'national_code',
'phone_number',
'age',
'avatar'
)
def create(self, validated_data):
user = User.objects.create_user(**validated_data)
return user
def update(self, instance, validated_data):
for (key, value) in validated_data.items():
setattr(instance, key, value)
instance.save()
return instance
login view:
class LoginView(ObtainAuthToken):
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
user = serializer.validated_data['user']
token, created = Token.objects.get_or_create(user=user)
return successful_response(
messages=_('User Login Successfully'),
data={
'token': token.key
}
)
return unsuccessful_response(errors=serializer.errors)
in postman, I send username and password and receive this response:
{
"errors": {
"non_field_errors": [
"Unable to log in with provided credentials."
]
}
}
I will send all the necessary parameters for logging in, I tried everything that came to my mind, but I could not solve the problem. I do not know the reason why I receive this error.Where does the problem come from I do not understand?
I Added password field in UserSerializer and solve this problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.