我想使用名为regroup
的Django标记对我的对象和关联的对象进行排序。这是我第一次使用此标签,并且需要您的帮助,因为我不会克服对查询集进行排序的问题。
我想得到类似的东西:
- Category 1
- Publication 1
- Document 1
- Document 2
- Publication 2
- Document 1
- Category 2
- Publication 3
- Document 1
- Category 3
- Publication 4
- Document 1
- Document 2
如您所见,对于每个类别,我都会显示相关的出版物。然后,对于普通出版物,我将显示相关文档。
我想使用regroup
标签来做到这一点。
我的 model.py 文件如下所示:
class Category(EdqmTable):
name = models.CharField(max_length=200, verbose_name=_('name'), unique=True, null=False)
class Publication(EdqmTable):
pub_id = models.CharField(max_length=10, verbose_name=_('publication ID'), unique=True, default='')
title = models.CharField(max_length=512, verbose_name=_('title'), null=False, unique=True)
category = models.ForeignKey(Category, verbose_name=_('category'), null=False, related_name='publication')
class Document(EdqmTable):
code = models.CharField(max_length=25, verbose_name=_('code'), unique=True, null=False, blank=False)
language = models.CharField(max_length=2, verbose_name=_('language'), choices=LANGUAGE_CHOICES, null=False, blank=False)
format = models.CharField(max_length=10, verbose_name=_('format'), choices=FORMAT_CHOICES, null=False, blank=False)
title = models.CharField(max_length=512, verbose_name=_('title'), null=False, blank=False)
publication = models.ForeignKey(Publication, verbose_name=_('publication title'), related_name='documents')
由于ForeignKey
:Category to Publication
和Publication to Document
,每个班级都受到了限制。
在我的 views.py 文件中:
# By default, display documents
test_research = Document.objects.all().order_by('publication__category__name')
kwargs['test_research'] = test_research
research_categories = defaultdict(list)
for element in test_research:
research_categories[element.publication.category].append(element)
research_publications = defaultdict(list)
for element in test_research:
research_publications[element.publication].append(element)
kwargs['test_research'] = test_research
kwargs['research_categories'] = research_categories
kwargs['research_publications'] = research_publications
我试图简化我的Django模板:
{% for category in research_categories|dictsort:'name' %}
{{ category }} <!-- Display categories -->
{% for document in test_research %} <!-- Display publications associated to each category -->
{% if document.publication.category|stringformat:"s" == category|stringformat:"s" %}
{{ document.publication }}
{% regroup document by document.publication as publication_list %} <!-- Display documents associated to each publication -->
<ul>
{% for document.publication in publication_list %}
<li>{{ document.publication.grouper }}
<ul>
{% for doc in document.publication.list %}
<li>{{ doc.title }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
{% endfor %}
我遇到了这个问题:
异常值:“文档”对象不可迭代
这是视图的打印内容:
#test_research
<QuerySet [<Document: document n°1>, <Document: TEST document 2>, <Document: document 1>, <Document: document 1>, <Document: document 1>]>
#research_categories
defaultdict(<class 'list'>, {<Category: CATEGORY N°1>: [<Document: document n°1>, <Document: TEST document 2>], <Category: CATEGORY N°2>: [<Document: document 1>], <Category: CATEGORY N°3>: [<Document: document 1>], <Category: TEST>: [<Document: document 1>]})
#research_publications
defaultdict(<class 'list'>, {<Publication: Publication n°1 (PUBSD-0001)>: [<Document: document n°1>, <Document: TEST document 2>], <Publication: Publication n°2 (PUBSD-0002)>: [<Document: document 1>], <Publication: Publication n°3 (PUBSD-0003)>: [<Document: document 1>], <Publication: Test validators (PUBSD-0004)>: [<Document: document 1>]})
非常感谢您能为我提供帮助