vabada
vabada

Reputation: 1814

How to properly create/update objects in Serializers from different apps

I am developing a REST API for my Django project. I have a working version of my project with two apps (accounts and sellers), but it performs all the logic in the AccountSerializer as seen here:

accounts/serializers.py

class AccountSerializer(serializers.ModelSerializer):
    seller = SellerSerializer(required=False)

    class Meta:
        model = Account
        fields = ('id', 'email', 'password', 'seller',)
        write_only_fields = ('password',)
        read_only_fields = ('id',)

    def create(self, validated_data):
        account = Account.objects.create(email=validated_data['email'])
        account.set_password(validated_data['password'])
        account.save()
        Seller.objects.create(account=account)

        return account

    def update(self, instance, validated_data):
        instance.email = validated_data.get('email', instance.email)
        instance.set_password(validated_data.get('password', instance.password))
        instance.save()

        seller = instance.seller
        seller_data = validated_data.pop('seller')
        seller.birth_date = seller_data.get('seller', seller.birth_date)
        seller.save()

        return instance

sellers/serializers.py

class SellerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Seller
        fields = ('birth_date',)

Since the logic related to the seller information is in the AccountSerializer, and I think it may make sense to transfer it to the SellerSerializer (in order to enhance decoupling) I have tried to perform all seller related modifications in the SellerSerializer, i.e. overriding the create and update methods.

I refactor the update methods as follows:

accounts/serializers.py

class AccountSerializer(serializers.ModelSerializer):
    seller = SellerSerializer(required=False)
    [...]
    def update(self, instance, validated_data):
        instance.email = validated_data.get('email', instance.email)
        instance.set_password(validated_data.get('password', instance.password))
        instance.save()

        # Update Seller related fields
        if 'seller' in validated_data:
            seller_data = validated_data['seller']
            self.seller.update(validated_data=seller_data)

sellers/serializers.py

class SellerSerializer(serializers.ModelSerializer):
    [...]
    def update(self, instance, validated_data):
        instance.birth_date = validated_data.get('birth_date',
                                                 instance.birth_date)
        instance.save()

    return instance

But I get the following error:

AttributeError: 'AccountSerializer' object has no attribute 'seller'

Same happens if I try to override the create method. I've searched but I haven't got any clue so far. Thanks!

Upvotes: 0

Views: 70

Answers (1)

PatNowak
PatNowak

Reputation: 5812

Why in your create method you use Seller instead of seller? Just use seller, which is your instance of SellerSerializer and it should be fine.

Upvotes: 1

Related Questions