3
好吧,它很簡單。 2機型多對多關係:訪問模板內的foreignkey
class Artist(models.Model):
name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True,
help_text='Uniq value for artist page URL, created from name')
birth_name = models.CharField(max_length=100, blank=True)
class Song(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True,
help_text='Unique value for product page URL, create from name.')
youtube_link = models.URLField(blank=False)
artists = models.ManyToManyField(Artist)
我看來想顯示最新的5首歌曲:
def songs(request, template_name="artists/songs.html"):
song_list = Song.objects.all().order_by('-created_at')[:5]
return render_to_response(template_name, locals(),
context_instance=RequestContext(request))
,問題是在模板...我想要顯示的藝術家姓名 只是不要真的知道如何做到這一點,我試過:
{% for song in song_list %}
{{ artists__name }} - {{ song.title }}
{% endfor %}
將不勝感激任何幫助!
@kaycee - 我已經更新了我的答案 - 你可以再試一次?另外,不要將其他查詢發佈爲答案,而是將它們添加爲您所指的答案下面的評論。 – 2010-01-13 10:55:11
非常感謝我的朋友!新手probs :) – kaycee 2010-01-13 10:57:22