This is smart solution, works like a charm. On Friday, September 4, 2015 at 4:48:14 PM UTC+8, Kaviraj Kanagaraj wrote: > > @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 > > On Wednesday, September 2, 2015 at 9:54:11 PM UTC+5:30, Kevin Brown wrote: >> >> 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). >> >> On Wed, Sep 2, 2015 at 12:09 PM Kaviraj Kanagaraj <[email protected]> >> wrote: >> >>> 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 [email protected]. >>> 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
