[Python] DRF getting AttributeError while trying to post data

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    I'm creating a user registration API in DRF using a Profile model with a OneToOne relationship with Django's default User Model. I have created a serializer with User model and defined the fields of Profile as serializer fields.

    The problem is when I post the data, though the user and profile are saved in the database I'm getting this error


    error AttributeError at /auth/registration Got AttributeError when attempting to get a value for field address on serializer UserRegistrationSerializer. The serializer field might be named incorrectly and not match any attribute or key on the User instance. Original exception text was: 'User' object has no attribute 'address'.


    using write_only with address seems to solve the problem but I want to understand why is this happening and how this view accessing this address field why it ignores the phone_number and date_of_birth and doesn't give me errors on those. Any input will be helpful. Thanks!

    Here is my code.

    models.py


    class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    date_of_birth = models.DateField(null=True)
    phone_number = models.CharField(null=True, max_length=30)
    address = models.CharField(max_length=250)

    def __str__(self):
    return f'Profile of {self.user.username}'

    Serializer.py


    class UserRegistrationSerializer(serializers.ModelSerializer):
    phone_number = serializers.CharField(required=False, max_length=30)
    date_of_birth = serializers.DateField(required=False)
    address = serializers.CharField(max_length=250)

    password = serializers.CharField(write_only=True)
    password2 = serializers.CharField(write_only=True, label = 'Confirm Password')
    class Meta:
    model = User
    fields = ['username', 'first_name', 'last_name', 'email', 'phone_number', 'date_of_birth', 'address', 'password', 'password2']

    def create(self, validated_data):
    phone_number = validated_data.pop('phone_number', None)
    date_of_birth = validated_data.pop('date_of_birth', None)
    address = validated_data.pop('address', None)
    password = validated_data.pop('password', None)
    first_name = validated_data.pop('first_name', None)
    last_name = validated_data.pop('last_name', None)

    user = User (
    username = validated_data['username'],
    email = validated_data['email'],
    first_name = first_name,
    last_name = last_name
    )

    user.set_password(password)
    user.save()

    Profile.objects.create(
    user=user,
    date_of_birth=date_of_birth,
    address=address,
    phone_number=phone_number
    )

    return user

    view.py


    class UserRegistrationView(generics.CreateAPIView):
    serializer_class = UserRegistrationSerializer
    queryset = User.objects.all()

    Continue reading...

Compartilhe esta Página