D
D
de_iiah_teji9_iiace2018-05-21 12:13:49
Django
de_iiah_teji9_iiace, 2018-05-21 12:13:49

What's wrong with the serializer?

My models

class Address(models.Model):
    street = models.CharField(max_length=100)
    city = models.CharField(max_length=100)
    zip_code = models.CharField(max_length=15)

class User(AbstractUser):
    phone = models.CharField(max_length=16, blank=True, null=True)

    shipping_address = models.ForeignKey(Address, on_delete=models.CASCADE, blank=True, null=True)
...

My serializer
class FinishUserRegistrationSerializer(serializers.HyperlinkedModelSerializer):
    street = serializers.ReadOnlyField(source='address.street')
    city = serializers.ReadOnlyField(source='address.city')
    zip_code = serializers.ReadOnlyField(source='address.zip_code')

    class Meta:
        model = User
        fields = (
            'street',
            'city',
            'zip_code',
            'email',
            'paypal_address',
            'ebay_id',
            'resellers_permit_number',
        )

    def update(self, instance, validated_data):
        address = Address.objects.create(
            street=validated_data['street'],
            city=validated_data['city'],
            zip_code=validated_data['zip_code']
        )
        instance.shipping_address = address
        instance.paypal_address = validated_data('paypal_address', instance.paypal_address)
        instance.ebay_id = validated_data('ebay_id', instance.ebay_id)
        instance.resellers_permit_number = validated_data('resellers_permit_number', instance.resellers_permit_number)
        instance.save()
        return instance

And my view
class UserFinishRegistrationView(GenericAPIView):
    serializer_class = FinishUserRegistrationSerializer

    def post(self, request):
        serializer = FinishUserRegistrationSerializer(data=request.data)
        if serializer.is_valid(raise_exception=True):
            user = User.objects.get(username=request.data['email'])
            if user:
                address = Address.objects.create(street=request.data['street'],
                                                 city=request.data['city'],
                                                 zip_code=request.data['zip_code'])
                json = serializer.data
                user.paypal_address = request.data['paypal_address']
                user.ebay_id = request.data['ebay_id']
                user.resellers_permit_number = request.data['resellers_permit_number']
                user.save()
                return Response(json, status=status.HTTP_201_CREATED)
            return Response(serializers.get_error_detail, status=status.HTTP_400_BAD_REQUEST)
        return Response(serializers.get_error_detail, status=status.HTTP_400_BAD_REQUEST)

Everything works fine, the address is created. Only for some reason does not assign it to the user. It is this line that does not work instance.shipping_address = address
. Through the shell, all the rules. What's wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question