参考官网:https://www.django-rest-framework.org/api-guide/relations/#nested-relationships 先建立model class Album(models.Model): album_name = models.CharField(max_length=) artist = models.CharField(max_length=) class Track(models.Model): album = models.Forei…
当需要对关联对象的字段进行选择性显示时,在所对应的serializer类中声明引用的字段. 例如: class TrackSerializer(serializers.ModelSerializer): class Meta: model = Track fields = ['order', 'title', 'duration'] class AlbumSerializer(serializers.ModelSerializer): tracks = TrackSerializer(many=…