0

Hi I want to deserialize only using 1 field. However, I want to serialize it as an object depending on the model.

Suppose I have:

#models.py
class Product(models.Model):
    name = models.CharField()
    amount = models.IntegerField()
    description = models.TextField()

class Query(models.Model):
    name = models.CharField()
    product = models.ForeignKey(Product)
    ...

#serializers.py
class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'

class QuerySerializer(serializers.ModelSerializer):
    product = ProductSerializer()

    class Meta:
        model = Query
        fields = '__all__'

I want to POST/deserialize something like this on the QuerySerializer:

{
    "name": "Query 1",
    "product": "Banana",
    ...
}

and I want something like this in return for serializer:

{
    "name": "Query 1",
    "product": {
                   "name": "Banana",
                   "amount": 1,
                   "description": "Banana description"
               }
    ...
}

I know a way is overriding to_internal_value but I do not like it since it messes up with ValidationErrrors.

I also get this as a result:

{'product': {'non_field_errors': 
['Invalid data. Expected a dictionary, but got str.']}}
Nikko
  • 1,410
  • 1
  • 22
  • 49
  • What do you expect to happen when you pass a payload as `{ "name": "Query 1", "product": "Banana", ... }` ? – JPG Feb 17 '20 at 05:48
  • Well there's only one product named "Banana", I want it get related to it. – Nikko Feb 17 '20 at 05:49

1 Answers1

1

First of all, make the name field of Product as unique to avoid unnecessary complications.

class Product(models.Model):
    name = models.CharField(unique=True)
    amount = models.IntegerField()
    description = models.TextField()

and change your serializer as,

class QuerySerializer(serializers.ModelSerializer):
    product = serializers.CharField(write_only=True)

    class Meta:
        model = Query
        fields = '__all__'

    def create(self, validated_data):
        product_name = validated_data.pop('product')
        product_instance = Product.objects.get(name=product_name)
        return Query.objects.create(product=product_instance, **validated_data)

    def to_representation(self, instance):
        rep = super().to_representation(instance)
        rep['product'] = ProductSerializer(instance.product).data
        return rep

Reference: DRF: Simple foreign key assignment with nested serializers?

JPG
  • 82,442
  • 19
  • 127
  • 206
  • I can write `to_representation` inside the ProductSerializer right? – Nikko Feb 17 '20 at 06:00
  • 1
    I didn't get you :( – JPG Feb 17 '20 at 06:01
  • Can I override `to_representation` of `ProductSerializer` instread of `QuerySerializer` to get the same result? – Nikko Feb 17 '20 at 06:03
  • 1
    Not in your case. If you do so, you will get the validation error as you've already mentioned in the OP. – JPG Feb 17 '20 at 06:04
  • 1
    Okay. Thank you! – Nikko Feb 17 '20 at 06:06
  • Hi, can I ask for more? Why is it not throwing a response error, if there's no data on the product such as `"product": "Apple"`. It is not throwing a response like: `{'product': ['Object with name=Apple does not exist.']}` – Nikko Feb 17 '20 at 06:30