Answer the question
In order to leave comments, you need to log in
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)
...
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
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)
instance.shipping_address = address
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question