我正在为一个教师网站编写一个小型 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 %}
但是,作者仍按字母顺序显示。我可以在我的模型/视图/模板中添加一些东西以使顺序正确,还是我必须借助列表和类似的东西来做?