Say I've two models:
class Singer((models.Model):
name = models.CharField(max_length=200)
class Song(models.Model):
title = models.CharField(max_length=200)
singer = models.ForeignKey(Singer)
And two serializers like:
class SingerSerializer(serializers.ModelSerializer):
class Meta:
model = Singer
fields = '__all__'
class SongSerializer(serializers.ModelSerializer):
singer = SingerSerializer()
class Meta:
model = Singer
fields = '__all__'
I've defined the serializers as above because I need the full foreign key object in the GET response for songs:
{
"singer": {
"id": 1,
"name": "foo"
},
"id": 1,
"title": "foo la la"
}
Is there a way to allow POST/PATCH payloads to only send in the id of the foreign object and not the entire object, without writing a different serializer? I'd like the PATCH payload to be so:
{
"singer": 1,
"id": 1,
"title": "foo la la"
}