Использование Django 2.1 / Django Rest Framework.
Я получаю вывод объекта Model из DRF вместо фактических полей.
Я хотел бы получить все элементы для таблиц audio_links и release_artists.
Вот что у меня есть.
Вывод
{
"title": "Attack The Dancefloor Volume Two",
"audiolinks": [
"AudioLinks object (55708)",
"AudioLinks object (55709)",
"AudioLinks object (55710)",
"AudioLinks object (55711)"
],
"releaseartists": [
"ReleaseArtists object (140)",
"ReleaseArtists object (141)"
]
}
models.py
class AudioLinks(models.Model):
release = models.ForeignKey('ReleasesAll', models.DO_NOTHING, db_column='release_id', related_name='audiolinks')
track_number = models.IntegerField()
class Meta:
managed = False
db_table = 'audio_links'
class ReleaseArtists(models.Model):
release = models.ForeignKey('ReleasesAll', models.DO_NOTHING, db_column='release_id', related_name='releaseartists')
artists = models.CharField(max_length=100)
class Meta:
managed = False
db_table = 'release_artists'
views.py
class ListReleaseDetailView(generics.RetrieveUpdateDestroyAPIView):
queryset = ReleasesAll.objects.all()
serializer_class = ReleasesSerializer
def get(self, request, *args, **kwargs):
try:
a_release = self.queryset.prefetch_related('releaseartists','audiolinks').get(pk=kwargs['release_id'])
return Response(ReleasesSerializer(a_release).data)
except ReleasesAll.DoesNotExist:
return Response(
data = {
"message": "{} does not exist".format(kwargs["release_id"])
},
status=status.HTTP_404_NOT_FOUND
)
serializers.py
class ReleasesSerializer(serializers.ModelSerializer):
audiolinks = serializers.StringRelatedField(many=True)
releaseartists = serializers.StringRelatedField(many=True)
class Meta:
model = ReleasesAll
fields = ('title','audiolinks','releaseartists')