0

我在models.py 中定义了一个名为Student 的自定义用户模型。这个模型继承了 Django 用户。我可以正确注册学生,但是当我想登录时,我得到了错误。

我想在学生注册时使用数据库中存在的身份编号和学生编号登录。

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)



class Student(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
    entry_year = models.PositiveIntegerField()
student_no = models.PositiveIntegerField()
serilizers.py:
  class CustomUserForLogin(serializers.ModelSerializer):
      class Meta:
         model = CustomUser
    fields = (
        'identity_no',
    )

  class StudentLoginView(serializers.ModelSerializer):
       user = CustomUserForLogin()

       class Meta:
          model = Student
          fields = [
            "user",
            "student_no", ]

def validate(self, data):  # validated_data
    identity_no = data.get('identity_no')
    print("identity_no", identity_no)
    student_no = data.get("student_no")
    print("student_no", student_no)
    # to search username or email is a user Model
    user = Student.objects.filter(
        Q(identity_no=identity_no) |
        Q(student_no=student_no)
    ).distinct()
    print("user", user)
    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

视图.py:

class StudentloginView(APIView):
    permission_classes = [AllowAny]
    serializer_class = StudentLoginView

    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = StudentLoginView(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

/system/student-login/ 的 FieldError

无法将关键字“identity_no”解析为字段。选项有:courserelationstudent、entry_year、id、student_no、user、user_id

请求方法:POST 请求 URL: http: //127.0.0.1 :8000/system/student-login/ Django 版本:1.11.17 异常类型:FieldError 异常值:

无法将关键字“identity_no”解析为字段。选项有:courserelationstudent、entry_year、id、student_no、user、user_id

异常位置:C:\Users\LELA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\query.py 在 names_to_path,第 1352 行 Python 可执行文件:C:\Users\ LELA\AppData\Local\Programs\Python\Python37\python.exe Python 版本:3.7.3 Python 路径:

['C:\Users\LELA\Desktop\APINewSystem', 'C:\Users\LELA\AppData\Local\Programs\Python\Python37\python37.zip', 'C:\Users\LELA\AppData\Local\Programs \Python\Python37\DLLs', 'C:\Users\LELA\AppData\Local\Programs\Python\Python37\lib', 'C:\Users\LELA\AppData\Local\Programs\Python\Python37', 'C :\Users\LELA\AppData\Local\Programs\Python\Python37\lib\site-packages']

服务器时间:2019 年 7 月 6 日星期六 05:37:50 +0000

4

1 回答 1

0

仔细看看学生过滤:

user = Student.objects.filter(
    Q(identity_no=identity_no) |
    Q(student_no=student_no)
).distinct()

然后在您的模型中:

class CustomUser(AbstractUser):
    ...
    identity_no = ...


class Student(models.Model):
    ...
    user = ...
    student_no = ...

字段identity_nostudent_no位于两个单独的模型中 -UserStudent. 因此,在您的学生过滤中,您应该对相关的用户模型执行过滤:

user = Student.objects.filter(
    Q(user__identity_no=identity_no) |  # <<<
    Q(student_no=student_no)
).distinct()
于 2019-07-06T08:13:00.043 回答