1👍
✅
The source
of fields in Django REST Framework can span relations, so you can get a property of a related object and use it for a field.
class MovieShowtimeSerializer(serializers.ModelSerializer):
title = serializers.CharField(source='movie.title')
theaterid = serializers.CharField(source='theater_id')
class Meta:
model = MovieShowtime
fields = ('movieid', 'theaterid','time', )
You must use the dotted path as the source
, so movie.title
will get you the title
of the related movie
. You may be interested in more fields though, in which case you are probably better of just nesting another serializer, like you have already done for the show times.
Source:stackexchange.com