我試圖從ManyToManyField
獲取數據,但發現不幸。你能幫我嗎? 這裏是我的Song App
型號。試圖從ManyToManyField獲取數據
class Artist(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(unique = True,max_length=100,help_text="Suggested value automatically generated from name. Must be unique.")
class Album(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(unique = True,max_length=100,help_text="Suggested value automatically generated from name. Must be unique.")
path = models.CharField(max_length=100,null=True, blank=True)
language = models.ForeignKey(Category)
albumid = models.CharField(max_length=100)
class Song(models.Model):
title = models.CharField(max_length=100)
artist = models.ManyToManyField(Artist)
music = models.ForeignKey(Music)
album = models.ForeignKey(Album)
這是我的看法。我想打印帶有標題的藝術家。 Titles
工作正常。但不是Artist
def movie_details(request,slug):
movies = get_object_or_404(Movie,slug=slug)
# calculating wikipedia directory structer for images
#image_name = movies.image
name = movies.title
album_name = Album.objects.get(name__exact=name)
album_id = album_name.id
song_list = Song.objects.filter(album=album_id)
#image_path = generateWikiImage(image_name)
#
return render_to_response('base_movies.html',locals(),
context_instance=RequestContext(request)
)
我試過這樣的模板。 {{song_list.artist}}
並得到這樣的消息<django.db.models.fields.related.ManyRelatedManager object at 0x024CBED0>
謝謝
謝謝它的工作。並顯示這樣的結果。 '[]」。我只想打印'SP。 Balasubramaniam'這可能嗎?謝謝 –
Kulbir
'[]'是一個列表,其中包含一個Artist對象。爲了正確地得到名字,遍歷這個列表中的東西並使用'{{artist.name}}'(如果你正在做'{%for artist in song.artist.all%}'')。 –
bouteillebleu