Skip to content Skip to sidebar Skip to footer

Foriegnkey Field Serializer In Django Rest Framework

Before posting this question I've read few questions on SOF. but they are from 2012 and very confusing as well. for e.g Django Rest Framework - Get related model field in serialize

Solution 1:

You just need to do:

classCarSerializer(serializers.ModelSerializer):
    user = UserSerializer()
    classMeta:
        model = Carfields= ('user','car_name')

that is all.

Also, you should take a look to the kindnesses of serializers.Field and serializers.SerializerMethodField, you can play with them and customize your response data as much as you wish.

As to the question around HyperlinkedModelSerializer-ModelSerializer, very clear here:

The HyperlinkedModelSerializer has the following differences from ModelSerializer:

  • It does not include the pk field by default.
  • It includes a url field, using HyperlinkedIdentityField.
  • Relationships use HyperlinkedRelatedField, instead of PrimaryKeyRelatedField.

Hope that helps.

Post a Comment for "Foriegnkey Field Serializer In Django Rest Framework"