0

Her it is my django serializer:

class ShopSerializer(serializers.ModelSerializer):
    rest = RestSerializer(many=True)
    class Meta:
        model = RestaurantLike
        fields = ('id', 'created', 'updated', 'rest')

This will wrap whole RestSerializer inside ShopSerializer on response. How can I get only one or two fields from RestSerializer instead of having all the fields inside ShopSerializer?

Get only two field of RestSerializer instead of whole RestSerializer

mehdi aria
  • 15
  • 4

1 Answers1

0

If you want to have limited amount of fields from RestSerializer, then you can simply limit it in the fields:

class RestSerializer(serializers.ModelSerializer):
    class Meta:
        model = Restaurant
        fields = ('id', 'name', ...)

If you want just want field, lets say the primary key or Slug, then you can use PrimaryKeyRelatedField or SlugRelatedField for that. For example:

class ShopSerializer(serializers.ModelSerializer):
    rest = serializers.SlugRelatedField(
        many=True,
        read_only=True,
        slug_field='name'
    )
    class Meta:
        model = RestaurantLike
        fields = ('id', 'created', 'updated', 'rest')
ruddra
  • 50,746
  • 7
  • 78
  • 101