148

I'm using the django rest framework to create an API. I have the following models:

class Category(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name


class Item(models.Model):
    name = models.CharField(max_length=100)
    category = models.ForeignKey(Category, related_name='items')

    def __unicode__(self):
        return self.name

To create a serializer for the categories I'd do:

class CategorySerializer(serializers.ModelSerializer):
    items = serializers.RelatedField(many=True)

    class Meta:
        model = Category

... and this would provide me with:

[{'items': [u'Item 1', u'Item 2', u'Item 3'], u'id': 1, 'name': u'Cat 1'},
 {'items': [u'Item 4', u'Item 5', u'Item 6'], u'id': 2, 'name': u'Cat 2'},
 {'items': [u'Item 7', u'Item 8', u'Item 9'], u'id': 3, 'name': u'Cat 3'}]

How would I go about getting the reverse from an Item serializer, ie:

[{u'id': 1, 'name': 'Item 1', 'category_name': u'Cat 1'},
{u'id': 2, 'name': 'Item 2', 'category_name': u'Cat 1'},
{u'id': 3, 'name': 'Item 3', 'category_name': u'Cat 1'},
{u'id': 4, 'name': 'Item 4', 'category_name': u'Cat 2'},
{u'id': 5, 'name': 'Item 5', 'category_name': u'Cat 2'},
{u'id': 6, 'name': 'Item 6', 'category_name': u'Cat 2'},
{u'id': 7, 'name': 'Item 7', 'category_name': u'Cat 3'},
{u'id': 8, 'name': 'Item 8', 'category_name': u'Cat 3'},
{u'id': 9, 'name': 'Item 9', 'category_name': u'Cat 3'}]

I've read through the docs on reverse relationships for the rest framework but that appears to be the same result as the non-reverse fields. Am I missing something obvious?

1

8 Answers 8

178

In the DRF version 3.6.3 this worked for me

class ItemSerializer(serializers.ModelSerializer):
    category_name = serializers.CharField(source='category.name')

    class Meta:
        model = Item
        fields = ('id', 'name', 'category_name')

More info can be found here: Serializer Fields core arguments

2
  • 5
    But you have to be ware cause it should threw NoneType error if the category field In the Item model is set to blank=True Oct 28, 2020 at 4:55
  • 4
    @DesertCamel You can simply add allow_null=True to your serializer CharField to overcome this
    – MohitC
    Dec 24, 2021 at 17:02
122

Just use a related field without setting many=True.

Note that also because you want the output named category_name, but the actual field is category, you need to use the source argument on the serializer field.

The following should give you the output you need...

class ItemSerializer(serializers.ModelSerializer):
    category_name = serializers.RelatedField(source='category', read_only=True)

    class Meta:
        model = Item
        fields = ('id', 'name', 'category_name')
11
  • 27
    what about retriving all the fields of catagory model??
    – A.J.
    Feb 19, 2014 at 11:55
  • 17
    if you want to retrieve all the fields of category model make the category serializer and have it in code like category_name = CategorySerliazer()
    – Faizan Ali
    Dec 1, 2014 at 11:31
  • 9
    I tried to do this but i'm getting a error Relational field must provide a 'queryset' argument, or set read_only='True'
    – ePascoal
    Jul 23, 2015 at 9:03
  • or providing a queryset attribute if you want to support create/update, something like: category_name = serializers.RelatedField(source='category', queryset=Category.objects.all()) I suppose.
    – stelios
    Oct 13, 2016 at 18:17
  • 1
    If you get AssertionError:... use this answer stackoverflow.com/a/44530606/5403449
    – Josh
    Apr 29, 2019 at 13:00
42

Another thing you can do is to:

  • create a property in your Item model that returns the category name and
  • expose it as a ReadOnlyField.

Your model would look like this.

class Item(models.Model):
    name = models.CharField(max_length=100)
    category = models.ForeignKey(Category, related_name='items')

    def __unicode__(self):
        return self.name

    @property
    def category_name(self):
        return self.category.name

Your serializer would look like this. Note that the serializer will automatically get the value of the category_name model property by naming the field with the same name.

class ItemSerializer(serializers.ModelSerializer):
    category_name = serializers.ReadOnlyField()

    class Meta:
        model = Item
0
30

this worked fine for me:

class ItemSerializer(serializers.ModelSerializer):
    category_name = serializers.ReadOnlyField(source='category.name')
    class Meta:
        model = Item
        fields = "__all__"
0
18

Simple solution source='category.name' where category is foreign key and .name it's attribute.

from rest_framework.serializers import ModelSerializer, ReadOnlyField
from my_app.models import Item

class ItemSerializer(ModelSerializer):
    category_name = ReadOnlyField(source='category.name')

    class Meta:
        model = Item
        fields = "__all__"
1
  • This is very well explained and easy to implement
    – DragonFire
    Dec 25, 2022 at 8:30
12

Worked on 08/08/2018 and on DRF version 3.8.2:

class ItemSerializer(serializers.ModelSerializer):
    category_name = serializers.ReadOnlyField(source='category.name')

    class Meta:
        model = Item
        read_only_fields = ('id', 'category_name')
        fields = ('id', 'category_name', 'name',)

Using the Meta read_only_fields we can declare exactly which fields should be read_only. Then we need to declare the foreign field on the Meta fields (better be explicit as the mantra goes: zen of python).

5

This solution is better because of no need to define the source model. But the name of the serializer field should be the same as the foreign key field name

class ItemSerializer(serializers.ModelSerializer):
    category = serializers.SlugRelatedField(read_only=True, slug_field='title')

    class Meta:
        model = Item
        fields = ('id', 'name', 'category')
0

For those that want to replace the field of the ForeignKey (that displays the ID) you can still used the __all__ syntax for convenience and simply over-write the field name as you see fit. For example:

class MyModelSerializer(serializers.ModelSerializer):

    # override the category field that would otherwise show an integer value 
    # for the ID with the field of that model you choose. "name" here.
    category = serializers.ReadOnlyField(source='category.name')

    class Meta:
        model = MyModel
        fields = '__all__'

IMO this is convenient b/c you can still use the __all__ syntax to capture any added fields later. Anything that's getting overridden is manually done so and, if needing to be reverted, can be manually done some without changing any other syntax.

Not the answer you're looking for? Browse other questions tagged or ask your own question.