Django多对多订购

时间:2013-09-04 21:07:49

标签: django many-to-many

我正在为教师网站编写一个小型CMS,其中包括最近的出版物列表。当然,在展示文章时,作者应该按字母顺序排序,但是,我似乎无法做到这一点。 以下是模型:

class Author(models.Model):
    initials = models.CharField(max_length=10)
    surname = models.CharField(max_length=30)
    university = models.CharField(max_length=100, verbose_name='University or organisation')
    web_address = models.URLField(verbose_name='Author''s webpage URL')
    email = models.EmailField(max_length=140, verbose_name='Author''s e-mail')

    def __unicode__(self):
        return u'%s %s' % (self.surname, self.initials)

    class Meta:
        ordering = ['surname']


class Article(models.Model):
    IS_PRINTED = (("Y", "In print"), ("N", "Accepted for publication"),)

    name = models.CharField(max_length=150, verbose_name="Article")
    authors = models.ManyToManyField(Author, verbose_name="Authors", through='Authored')
    journal = models.CharField(max_length=80, verbose_name="Journal")
    status = models.CharField(choices=IS_PRINTED, max_length=1, verbose_name="Published or accepted for publication")
    year = models.IntegerField()
    volume = models.IntegerField(blank=True, null=True)
    no = models.IntegerField(blank=True, null=True, verbose_name="No")
    start_page = models.IntegerField(blank=True, null=True)
    end_page = models.IntegerField(blank=True, null=True)
    web_address = models.URLField(blank=True, verbose_name='Article webpage URL')

    class Meta:
        ordering = ['year']

    def __unicode__(self):
        return self.name


class Authored(models.Model):
    AUTHOR_NO = ((1, "1"), (2, "2"), (3, "3"), (4, "4"), (5, "5"), (6, "6"), (7, "7"),)

    author = models.ForeignKey(Author)
    article = models.ForeignKey(Article)
    author_ord = models.IntegerField(choices=AUTHOR_NO)

    class Meta:
        ordering = ['author_ord']

为了显示这一点,我在视图中执行以下操作:

articles = Article.objects.all().reverse()

return render(request, 'articles.html', {'recent_posts': post_names, 'plaintext': plaintext.text,
                                             'articles': articles})

以下是模板中用于显示文章的代码段:

{% for article in articles %}
    {% for author in article.authors.all  %}
        {{ author.surname }},
    {% endfor %}
    {{ article.name }}. // {{ article.journal }}. — {{ article.year }}.
    {% if article.volume %}
        — Vol. {{ article.volume }},
    {% endif %}
    {% if article.no %}
        no. {{ article.no }}
    {% endif %}
    {% if article.start_page %}
        p. {{ article.start_page }}-{% endif %}
    {% if article.end_page %}
        {{article.end_page }}.
    {% endif %}
    {% if article.web_address %}
        <a href={{article.web_address}}>Link to article text</a>
    {% endif %} <br />
{% endfor %}

然而,作者仍然按字母顺序显示。 我可以在我的模型/视图/模板中添加一些内容以使订单正确,或者我是否必须借助列表和类似的东西来执行此操作?

1 个答案:

答案 0 :(得分:4)

我认为这里的问题是您的模板正在访问article.authors,其中surname排序。它是Authored模型,具有您所需的顺序,因此请尝试:

{% for authored in article.authored_set.all %}
    {{ authored.author.surname }},
{% endfor %}