显示具有不同名称的多对多子集?在Django模板中

时间:2011-04-19 07:57:53

标签: django many-to-many

我计划在下次重建数据库时删除相关名称......我使用的模型更多是测试模型。那么,对于类创建者和作家,cover_artist等,我将如何展示创作者创建的问题(一旦我删除相关名称,除非有办法绕过它)?

class Creator(models.Model):
    name = models.CharField(max_length=256)
    desc = models.TextField("description", blank=True, null=True)
    #type = writer, artist, editor, letterer
    slug = models.SlugField(blank=True, null=True)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ['name']
    def get_absolute_url(self):
        return "/comics2/creators/%s" % self.slug 


class Issue(models.Model):
    ....
    writer = models.ManyToManyField(Creator, related_name="written by", help_text="Use cmd/ctrl + click to select multiple names. The same applies to the rest of the creators below.", blank=True, null=True)
    artist = models.ManyToManyField(Creator, related_name="drawn by", blank=True, null=True)
    cover_artist = models.ManyToManyField(Creator, related_name="cover drawn by", blank=True, null=True)
    colorist = models.ManyToManyField(Creator, related_name="colored by", blank=True, null=True)
    inker = models.ManyToManyField(Creator, related_name="inked by", blank=True, null=True)
    penciller = models.ManyToManyField(Creator, related_name="pencilled by", blank=True, null=True)
    letterer = models.ManyToManyField(Creator, related_name="lettered by", blank=True, null=True)
    editor = models.ManyToManyField(Creator, related_name="edited by", blank=True, null=True)
    arc = models.ManyToManyField(Arc, related_name="related story arc", blank=True, null=True)
    ...
    def __unicode__(self):
        return u'%s %s' % (self.title, self.number)
    def get_absolute_url(self):
        return "/comics2/issues/%s" % self.slug     
    class Meta:
        ordering = ['title', 'number']
    def get_stars(self):
        star_rating = self.rating * 16
        return star_rating

...

{% for issue in creator.____?__.all %}
<ul>
<li>{{ issue }}</li>
</ul>    
{% endfor %}

不会起作用。

感谢。

1 个答案:

答案 0 :(得分:0)

我建议更改你的models.py让问题通过另一个表与Creator有多对多的关系,例如:角色。请在此处查看docs

class Creator(models.Model):
    name = models.CharField(max_length=256)
    desc = models.TextField("description", blank=True, null=True)
    slug = models.SlugField(blank=True, null=True)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ['name']
    def get_absolute_url(self):
        return "/comics2/creators/%s" % self.slug 

class Issue(models.Model):
    creators = models.ManyToManyField(Creator, through='Role')
    arc = models.ManyToManyField(Arc, related_name="related story arc", blank=True, null=True)
    ...
    def __unicode__(self):
        return u'%s %s' % (self.title, self.number)
    def get_absolute_url(self):
        return "/comics2/issues/%s" % self.slug     
    class Meta:
        ordering = ['title', 'number']
    def get_stars(self):
        star_rating = self.rating * 16
        return star_rating

class Role(models.Model):
    issue = models.ForeignKey(Issue)
    creator = models.ForeignKey(Creator)
    role = models.CharField()
    ...

编辑:

在您的问题视图中,您将获得不同的角色并将其传递给您的模板(例如):

def issue_detail(request, issue_slug=None):
    issue = get_object_or_404(Issue, slug=creator_slug)
    writers = issue.creators.filter(role__role='writer')
    cover_artists = issue.creators.filter(role__role='cover artist')
    ...

    context = { 'issue': issue,
                'writers': writers,
                'cover_artists': cover_artists,
                ...
               }

    return render_to_response('issue_detail.html', context,
           context_instance=RequestContext(request))

模板:

<label>Writers</label>
{% for writer in writers %}
    {{ writer }}
{% endfor %}
<label>Cover Artists</label>
{% for cover_artist in cover_artists %}
    {{ cover_artist }}
{% endfor %}

创作者观点:

def creator_detail(request, issue_slug=None):
    creator = get_object_or_404(Creator, slug=issue_slug)
    issues_as_writer = creator.issue_set.filter(role__role='writer')
    issues_as_cover_artists = creator.issue_set.filter(role__role='cover artist')

    #all issues related to this creator
    all_issues = creator.issue_set.all()
    ...

    context = { 'creator': creator,
                'issues_as_writer': issues_as_writer,
                'issues_as_cover_artist': issues_as_cover_artist,
                ...
                'all_issues': all_issues,
               }

    return render_to_response('creator_detail.html', context,
           context_instance=RequestContext(request))