0

I have the following Django model structure:

class TypeOfIngredient(models.Model):
    name = models.CharField(max_length=200,unique=True,null=False)
    slug = models.SlugField(unique=True)

class Ingredient(models.Model):
    name = models.CharField(max_length=200,unique=True,null=False)
    slug = models.SlugField(unique=True)
    typeofingredient = models.ForeignKey(TypeOfIngredient, related_name='typeof_ingredient',null=True, blank=True,on_delete=models.PROTECT)

Serializer:

class IngredientListSerializer(ModelSerializer):
    class Meta:
        model = Ingredient
        fields = '__all__'

With the above serializer i see the following api output:

"results": [
        {
            "id": 1,
            "name": "adrak",
            "slug": "adrak",
            "typeofingredient": null
        },
        {
            "id": 2,
            "name": "banana",
            "slug": "banana",
            "typeofingredient": 1
        },

How to get "typeofingredient": "fruit" where fruit is the name field of the typeofingredient. What i am getting is the id.

I tried nested:

class IngredientListSerializer(ModelSerializer):
    class Meta:
        model = Ingredient
        fields = '__all__'
        depth = 1

Then i get the api output as:

"results": [
        {
            "id": 1,
            "name": "adrak",
            "slug": "adrak",
            "typeofingredient": null
        },
        {
            "id": 2,
            "name": "banana",
            "slug": "banana",
            "typeofingredient": {
                    "id": 1,
                    "name": "fruit",
                    "slug": "fruit"
            }
        },

Here is showing all the details of the typeofingredient. Rather than this can i have directly "typeofingredient": "fruit"

2 Answers 2

1

Use serializers.ReadOnlyField

class IngredientListSerializer(ModelSerializer):
    typeofingredient = serializers.ReadOnlyField(source='typeofingredient.name')    

    class Meta:
        model = Ingredient
        fields = '__all__'
Sign up to request clarification or add additional context in comments.

2 Comments

Can you guide me why we use ReadOnlyField. I am quite new to serializers
A field class that simply returns the value of the field without modification. ReadOnlyField
1

You can add str method on models.py

class TypeOfIngredient(models.Model):
    name = models.CharField(max_length=200,unique=True,null=False)
    slug = models.SlugField(unique=True)

    def __str__(self):
        return str(self.name)

class Ingredient(models.Model):
    name = models.CharField(max_length=200,unique=True,null=False)
    slug = models.SlugField(unique=True)
    typeofingredient = models.ForeignKey(TypeOfIngredient, related_name='typeof_ingredient',null=True, blank=True,on_delete=models.PROTECT)

4 Comments

this is not what the op want
he wants name instead of id... it returns the TypeOfIngredient name
well it may solve the problem but not a drf thing, it would be better to offer serializers approach
I am looking for serialzer approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.