Django модель многие ко многим через не работает
у меня есть база данных mysql, которая уже создана
.
поэтому models.py создан manage.py inspectdb
.
и я добавляю artists = models.ManyToManyField(Artist, related_name='songs', through = "ArtistSong")
в таблицу Song вот так.
class Artist(models.Model):
artist_id = models.IntegerField(primary_key=True)
artist_name = models.TextField(blank=True, null=True)
artist_main_genre = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'artist'
class Song(models.Model):
song_id = models.IntegerField(primary_key=True)
issue_date = models.TextField(blank=True, null=True)
album_name = models.TextField(blank=True, null=True)
album_id = models.IntegerField(blank=True, null=True)
song_name = models.TextField(blank=True, null=True)
added_cnt = models.IntegerField(blank=True, null=True)
thumb_url = models.TextField(blank=True, null=True)
artists = models.ManyToManyField(Artist, related_name='songs', through = "ArtistSong")
class Meta:
managed = False
db_table = 'song'
class ArtistSong(models.Model):
song = models.ForeignKey(Song, models.DO_NOTHING, blank=True, null=True)
artist = models.ForeignKey(Artist, models.DO_NOTHING, blank=True, null=True)
class Meta:
managed = False
db_table = 'artist_song'
я хочу получить название и имя исполнителя песни. мой views.py здесь.
class Songs(APIView):
def get(self, request, *args, **kwargs):
song_list= request.GET.get('song_list')
queryset = Song.objects.filter(song_id__in = song_list)
rec_songs = []
for song in queryset.iterator():
#for checking
print(song.song_name)
print(song.artists.all())
data = {
'song_name' : song.song_name,
'artist_name': [a.artist_name for a in song.artists.all()],
}
rec_songs.append(data)
return Response(json.dumps(rec_songs,ensure_ascii = False))
print(song.song_name)
показывает название песни, как я и предполагал.
print(song.artists.all())
не показывает ничего, кроме <QuerySet []>
прошу помощи!