0

I was using a standard Django user model but then requirements changed and I had to resort to an abstract user model. I updated my serializer to reflect the changes in model fields as well as form fields but I'm now getting an error when attempting to creating a user object. The error message is KeyError: 'unl_email' but also shows up for all the other fields. The validated_data for the create() function in serializers.py appears to be empty when I print. What am I doing wrong?

Views.py

class SignUp(CreateAPIView):
    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

    def create(self, request, *args, **kwargs):
        serializer = SignUpSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        return Response({"success": "Registration succesful"}, status=status.HTTP_201_CREATED)

    def perform_create(self, serializer):
        serializer.save()

Models.py

class User(AbstractUser):
    unl_num = models.AutoField(primary_key=True)
    unl_usu_num = models.IntegerField(blank=True,null =True)
    unl_date_added = models.DateField(default=timezone.now)
    unl_name = models.CharField(max_length=256)
    unl_surname = models.CharField(max_length=256)
    unl_email = models.EmailField(max_length=200, unique=True)
    unl_mobile = models.CharField(max_length=256)
    unl_identity= models.CharField(max_length=256)
    unl_oss_num= models.CharField(max_length=256)
    unl_race= models.CharField(max_length=32)
    unl_gender= models.CharField(max_length=32)
    unl_disability= models.CharField(max_length=32)
    unl_location= models.CharField(max_length=256)
    unl_province= models.CharField(max_length=256)
    unl_avatar= models.CharField(max_length=256)

    USERNAME_FIELD = 'unl_email'

Serializers.py

class SignUpSerializer(serializers.Serializer):
    class Meta:
        model = User
        fields = ('unl_email','password','unl_name',
                 'unl_surname','unl_mobile','unl_identity',
                 'unl_oss_num','unl_race','unl_gender','unl_disability',
                 'unl_location','unl_province','unl_avatar'
                 )

        password = serializers.CharField(write_only=True)

    def create(self, validated_data):
        user = User.objects.create(
            unl_email=validated_data['unl_email'],
            unl_name=validated_data['unl_name'],
            unl_surname=validated_data['unl_surname'],
            unl_mobile=validated_data['unl_mobile'],
            unl_identity=validated_data['unl_identity'],
            unl_oss_num=validated_data['unl_oss_num'],
            unl_race=validated_data['unl_race'],
            unl_gender=validated_data['unl_gender'],
            unl_disability=validated_data['unl_disability'],
            unl_location=validated_data['unl_location'],
            unl_province=validated_data['unl_province'],
            unl_avatar=validated_data['unl_avatar']
        )
        user.set_password(validated_data['password'])
        user.save()

        return user
1
  • call serializer.save() after checking if serializer.is_valid(raise_exception=True): else return serializer.errors and add the errors in your post. Commented Jan 24, 2020 at 9:25

1 Answer 1

2

You're using serializers.Serializer and then specifying a model attribute for that serializer, which it does not use. Change it to serializers.ModelSerializer instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.