Discussion:
Using the same serializer field as PrimaryKeyRelatedField for write and as NestedSerializer Field for read
Kaviraj Kanagaraj
2015-09-02 16:09:04 UTC
Permalink
Hi Everyone,

Conside the following case

class Album(models.Model):
name = models.CharField(max_length=20)

class Track(models.Model):
album = models.ForeignKey(Album, related_name='tracks')


I need to create TrackSerializer,

class TrackSeriallizer(serializers.ModelSerializer):
class Meta:
model = Track

Here by default, album field will be treated as PrimayKeyRelatedField. So
the serialized data would some like

{
'id': 1,
'album':1
}

but I wanted to be
{
'id': 1,
'album': {
'id': 1,
'name': 'album1'
}
}

I am aware that I could use nested serializer as below to achive this, but
then I cannot create Track instance just by passing album id

class TrackSeriallizer(serializers.ModelSerializer):
album = AlbumSerializer()
class Meta:
model = Track


Can I create a TrackSerializer, so that whenever I get a list of Tracks I
wants Album for the track to be nested. But while creating a Track, I
should be able to pass album as id(PrimaryKeyRelation)?

Help me figuring out. Thanks in advance
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Kevin Brown
2015-09-02 16:24:00 UTC
Permalink
It's probably possible to do this, but usually I would recommend just
having two separate fields (as covered in this Stack Overflow answer
<http://stackoverflow.com/q/29950956/359284#29953188>). One holds the
primary key (can be write-only) and the other is the nested serializer
(usually read-only).

Most of the alternatives either require a lot of work (overriding
`to_internal_value`) or result in inconsistent responses (custom
serializer).
Post by Kaviraj Kanagaraj
Hi Everyone,
Conside the following case
name = models.CharField(max_length=20)
album = models.ForeignKey(Album, related_name='tracks')
I need to create TrackSerializer,
model = Track
Here by default, album field will be treated as PrimayKeyRelatedField. So
the serialized data would some like
{
'id': 1,
'album':1
}
but I wanted to be
{
'id': 1,
'album': {
'id': 1,
'name': 'album1'
}
}
I am aware that I could use nested serializer as below to achive this, but
then I cannot create Track instance just by passing album id
album = AlbumSerializer()
model = Track
Can I create a TrackSerializer, so that whenever I get a list of Tracks I
wants Album for the track to be nested. But while creating a Track, I
should be able to pass album as id(PrimaryKeyRelation)?
Help me figuring out. Thanks in advance
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Kaviraj Kanagaraj
2015-09-04 08:48:13 UTC
Permalink
@Kevin Thanks for your help.

I have solved this by following way,

class TrackSeriallizer(serializers.ModelSerializer):
album = AlbumSerializer(read_only=True)
album_id =
serializers.PrimaryKeyRelatedField(queryset=Album.objects.all(),
write_only=True, source='album')

class Meta:
model = Track

Now during listing of Tracks 'album' field will be used as nested
serializer, And during create, album_id is used with source 'as album'. So
that I can just pass the album_id during creation. 'source' attribute of
the serializer solves the problem
Post by Kevin Brown
It's probably possible to do this, but usually I would recommend just
having two separate fields (as covered in this Stack Overflow answer
<http://stackoverflow.com/q/29950956/359284#29953188>). One holds the
primary key (can be write-only) and the other is the nested serializer
(usually read-only).
Most of the alternatives either require a lot of work (overriding
`to_internal_value`) or result in inconsistent responses (custom
serializer).
Post by Kaviraj Kanagaraj
Hi Everyone,
Conside the following case
name = models.CharField(max_length=20)
album = models.ForeignKey(Album, related_name='tracks')
I need to create TrackSerializer,
model = Track
Here by default, album field will be treated as PrimayKeyRelatedField. So
the serialized data would some like
{
'id': 1,
'album':1
}
but I wanted to be
{
'id': 1,
'album': {
'id': 1,
'name': 'album1'
}
}
I am aware that I could use nested serializer as below to achive this,
but then I cannot create Track instance just by passing album id
album = AlbumSerializer()
model = Track
Can I create a TrackSerializer, so that whenever I get a list of Tracks I
wants Album for the track to be nested. But while creating a Track, I
should be able to pass album as id(PrimaryKeyRelation)?
Help me figuring out. Thanks in advance
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mingda Zhao
2018-07-15 13:16:34 UTC
Permalink
THIS IS FREAKING MAGIC
Post by Kaviraj Kanagaraj
@Kevin Thanks for your help.
I have solved this by following way,
album = AlbumSerializer(read_only=True)
album_id =
serializers.PrimaryKeyRelatedField(queryset=Album.objects.all(),
write_only=True, source='album')
model = Track
Now during listing of Tracks 'album' field will be used as nested
serializer, And during create, album_id is used with source 'as album'. So
that I can just pass the album_id during creation. 'source' attribute of
the serializer solves the problem
Post by Kevin Brown
It's probably possible to do this, but usually I would recommend just
having two separate fields (as covered in this Stack Overflow answer
<http://stackoverflow.com/q/29950956/359284#29953188>). One holds the
primary key (can be write-only) and the other is the nested serializer
(usually read-only).
Most of the alternatives either require a lot of work (overriding
`to_internal_value`) or result in inconsistent responses (custom
serializer).
Post by Kaviraj Kanagaraj
Hi Everyone,
Conside the following case
name = models.CharField(max_length=20)
album = models.ForeignKey(Album, related_name='tracks')
I need to create TrackSerializer,
model = Track
Here by default, album field will be treated as PrimayKeyRelatedField.
So the serialized data would some like
{
'id': 1,
'album':1
}
but I wanted to be
{
'id': 1,
'album': {
'id': 1,
'name': 'album1'
}
}
I am aware that I could use nested serializer as below to achive this,
but then I cannot create Track instance just by passing album id
album = AlbumSerializer()
model = Track
Can I create a TrackSerializer, so that whenever I get a list of Tracks
I wants Album for the track to be nested. But while creating a Track, I
should be able to pass album as id(PrimaryKeyRelation)?
Help me figuring out. Thanks in advance
--
You received this message because you are subscribed to the Google
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Joel Kumwenda
2018-08-08 02:59:27 UTC
Permalink
This is smart solution, works like a charm.
Post by Kaviraj Kanagaraj
@Kevin Thanks for your help.
I have solved this by following way,
album = AlbumSerializer(read_only=True)
album_id =
serializers.PrimaryKeyRelatedField(queryset=Album.objects.all(),
write_only=True, source='album')
model = Track
Now during listing of Tracks 'album' field will be used as nested
serializer, And during create, album_id is used with source 'as album'. So
that I can just pass the album_id during creation. 'source' attribute of
the serializer solves the problem
Post by Kevin Brown
It's probably possible to do this, but usually I would recommend just
having two separate fields (as covered in this Stack Overflow answer
<http://stackoverflow.com/q/29950956/359284#29953188>). One holds the
primary key (can be write-only) and the other is the nested serializer
(usually read-only).
Most of the alternatives either require a lot of work (overriding
`to_internal_value`) or result in inconsistent responses (custom
serializer).
Post by Kaviraj Kanagaraj
Hi Everyone,
Conside the following case
name = models.CharField(max_length=20)
album = models.ForeignKey(Album, related_name='tracks')
I need to create TrackSerializer,
model = Track
Here by default, album field will be treated as PrimayKeyRelatedField.
So the serialized data would some like
{
'id': 1,
'album':1
}
but I wanted to be
{
'id': 1,
'album': {
'id': 1,
'name': 'album1'
}
}
I am aware that I could use nested serializer as below to achive this,
but then I cannot create Track instance just by passing album id
album = AlbumSerializer()
model = Track
Can I create a TrackSerializer, so that whenever I get a list of Tracks
I wants Album for the track to be nested. But while creating a Track, I
should be able to pass album as id(PrimaryKeyRelation)?
Help me figuring out. Thanks in advance
--
You received this message because you are subscribed to the Google
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...