I user Custom User Model named Student which inherits Django User Model. I have problem in its logon when I want to use check_password. The error is that Student which is a custom user model has not such attribute.
I wanna login students with the registered information by them. and the fields to login is identity_no and student_no.
models.py:
class CustomUser(AbstractUser):
USER_TYPE_CHOICES = ((1, 'student'),
(2, 'professor'),)
username = models.CharField(max_length=50, unique=True)
user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES, null=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=100)
identity_no = models.PositiveIntegerField(default=0)
email = models.EmailField(max_length=300)
date_joined = models.DateTimeField('date joined', default=timezone.now)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
class Student(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
entry_year = models.PositiveIntegerField()
student_no = models.PositiveIntegerField()
def get_full_name(self):
return self.user.first_name + self.user.last_name
def __unicode__(self):
return self.get_full_name()
Serializers.py:
class StudentLoginSerializer(serializers.ModelSerializer): user = CustomUserSerializerForLogin()
class Meta:
model = Student
fields = [
"user",
"student_no", ]
def validate(self, data): # validated_data
user_data = data.pop('user', None)
identity_no = user_data.get('identity_no')
print("identity_no", identity_no)
student_no = data.get("student_no")
user = Student.objects.filter(
Q(user__identity_no=identity_no) |
Q(student_no=student_no)
).distinct()
# user = user.exclude(user__identity_no__isnull=True).exclude(user__identity_no__iexact='')
if user.exists() and user.count() == 1:
user_obj = user.first()
else:
raise ValidationError("This username or student_no is not existed")
if user_obj:
if not user_obj.check_password(student_no): # Return a boolean of whether the raw_password was correct.
raise ValidationError("Incorrect Credential please try again")
return user_obj
Sent from Mail for Windows 10
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5d218062.1c69fb81.f85a6.42f3%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.