Hi Jason, Thank you fo rthe quick reply. Did you mean to create XyzSerializer like this?
class XyzSerializer(serializers.ModelSerializer): tracks = TrackSerializer(many=True, read_only=True) when you said "do the same in TrackSerializer" did you mean 'reverse relation'? How to do reverse relation in this case? Best, Leif On Wednesday, March 14, 2018 at 11:14:49 AM UTC-4, Leif wrote: > > Dear Django, > > I have a question about django rest framework relations. I am looking at > the example in > http://www.tomchristie.com/rest-framework-2-docs/api-guide/relations. In > my situation there is additional class called Xyz: > > class Xyz(models.Model): > xyz_name = models.CharField(max_length=100) > name2 = models.CharField(max_length=100) > > class Album(models.Model): > album_name = models.CharField(max_length=100) > artist = models.CharField(max_length=100) > > class Track(models.Model): > album = models.ForeignKey(Album, related_name='tracks') > xyz = models.ForeignKey(Xyz, related_name=‘xyz’) > order = models.IntegerField() > title = models.CharField(max_length=100) > duration = models.IntegerField() > > here is the example of serializer in the link above. > > class TrackSerializer(serializers.ModelSerializer): > class Meta: > model = Track > fields = ('order', 'title') > > class AlbumSerializer(serializers.ModelSerializer): > tracks = TrackSerializer(many=True, read_only=True) > > class Meta: > model = Album > fields = ('album_name', 'artist', 'tracks') > > My question is how to include XyzSerializer(serializers.ModelSerializer) > so all three objects will be included in json output. > > Thank you very much in advance, > > Leif > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/029061f7-10ad-4681-a8b5-7900262bdeb0%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.