Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem of pass through values to nested serializers from the call to the base serializer's save method. #179

Open
playma opened this issue Jul 28, 2023 · 3 comments

Comments

@playma
Copy link

playma commented Jul 28, 2023

I want to pass values to nested serializers that have changed names, but it seems not to work. Can somebody help me?

Serializer

class PriceSerializer(PriceBaseSerializer):
    class ProductSerializer(serializers.ModelSerializer):
        class Meta:
            model = Product
            fields = ['id', 'merchant', 'name', 'description', 'images', 'metadata', 'is_livemode']
            extra_kwargs = {
                'is_livemode': {'required': False},
                'merchant': {'required': False},
            }

    product_data = ProductSerializer(source='product', required=False)

    def validate(self, attrs):
        if not attrs.get('product') and not attrs.get('product_data'):
            raise serializers.ValidationError('product or product_data is required')

        return attrs

    class Meta:
        model = Price
        fields = [
            'id',
            'merchant',
            'product',
            'product_data',
            'currency',
            'unit_amount',
            'metadata',
            'is_livemode',
            'payment_link_count',
        ]
        read_only_fields = ['payment_link_count']
        extra_kwargs = {
            'product': {'required': False},
            'is_livemode': {'required': False},
            'merchant': {'required': False},
        }

Save function 1

serializer.save(
    product_data={
        'merchant_id': self.merchant_id,
        'is_livemode': self.is_livemode,
    },
    merchant_id=self.merchant_id,
    is_livemode=self.is_livemode,
)

Error happens 1

TypeError: Price() got an unexpected keyword argument 'product_data'

Save function 2

serializer.save(
    product={
        'merchant_id': self.merchant_id,
        'is_livemode': self.is_livemode,
    },
    merchant_id=self.merchant_id,
    is_livemode=self.is_livemode,
)

Error happens 2 (Not pass value successfully)

django.db.utils.IntegrityError: null value in column "is_livemode" of relation "billing_product" violates not-null constraint
@playma
Copy link
Author

playma commented Jul 28, 2023

@ruscoder
Copy link
Member

Hi @playma! There was a similar issue #22 that was fixed a long time ago and also covered with tests. I really surprised that it does not work in your case. Could you please share a piece of your code of PriceBaseSerializer where drf-writable-nested actually mixed?

@ruscoder
Copy link
Member

@playma I've reviewed your PR, indeed, it makes sense to check field_name in validated_data instead of field.source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants