Django模板 - 打印逗号分隔的ManyToManyField,将结果列表排序成dict? [英] Django Templates - Printing Comma-separated ManyToManyField, sorting results list into dict?
问题描述
文章
。这有各种各样的领域来存储文章的标题,出版日期,主题以及文章中提及的公司名单。 (公司
是自己的模型)。 我想要一个打印出文章列表的模板,排序按类别划分,并列出所提及的公司。
但是,我遇到两个问题。
,公司
字段是一个ManyToMany字段。我现在打印成功,使用 all
iterable,谢谢这个SO问题=)。 (好奇,但是,Django文档中记录的 all
iterable在哪里?)
但是,我想在每个项目之后打印,(逗号后跟空格),除了最后一个项目。所以输出将是:
Joe Bob公司,Sarah Jane公司,工具公司
而不是:
Joe Bob公司, Sarah Jane Company,Tool Company,
如何使用Django的模板系统实现?
其次,每个文章
有一个CharField,称为类别
文章的类别。如果可能,我想按类别排序的文章。所以我使用QuerySet,并在article_list中获得一个很好的相关文章列表。然后我使用重组
模板标签将其分类并打印每个。
{'tennis':('article_4','article_5')
'cricket':('article_2','article_3')
'乒乓':('article_1')
}
但是,我需要确保我的输入列表被排序,之后我通过它到重组
。我的问题是,最好使用 dractionsort
template-tag在模板中排序,或者我应该使用QuerySet的 order_by
调用?
我认为使用重新组合
更好,而不是试图自己编写代码在Python里面的视图?
干杯,
维克多
第一个问题
使用像连接过滤器这样的python
{{article.company.all | join:,}}
http://docs.djangoproject.com/en/dev/ref / templates / builtins /#join
第二个问题
我的问题是,最好使用
dictsort模板标签来对模板中的
进行排序,还是应该使用
QuerySet的order_by调用tead?
我将使用QuerySet的order_by。我喜欢在DB中做这样的事情。使用一个巨大的数据集可以使用数据库索引。
我认为最好使用重组,而不是尝试编码
这个我自己在Python里面的视图?
重组。使用python本机功能是非常好的。
I have a Django project for managing a list of journal articles. The main model is Article
. This has various fields to store things like title of the article, publication date, subject, as well as list of companies mentioned in the article. (company
is it's own model).
I want a template that prints out a list of the articles, sorted by category, and also listing the companies mentioned.
However, I'm hitting two issues.
Firstly, the company
field is a ManyToMany field. I'm printing this successfully now, using the all
iterable, thanks to this SO question =). (Curious though, where is this all
iterable documented in the Django documentation?)
listing objects from ManyToManyField
However, I'd like to print ", " (comma followed by space) after each item, except the last item. So the output would be:
Joe Bob Company, Sarah Jane Company, Tool Company
and not:
Joe Bob Company, Sarah Jane Company, Tool Company,
How do you achieve this with Django's templating system?
Secondly, each Article
has a CharField, called category
, that stores the category for the article. I would like the articles sorted by Categories, if possible. So I use QuerySet, and get a nice list of relevant articles in article_list. I then use the regroup
template tag to sort this into categories and print each one.
{ 'tennis': ('article_4', 'article_5')
'cricket': ('article_2', 'article_3')
'ping pong': ('article_1')
}
However, I need to make sure that my input list is sorted, before I pass it to regroup
. My question is, is it better to use the dictsort
template-tag to sort this inside the template, or should I use QuerySet's order_by
call instead?
And I assume it's better to use regroup
, rather than trying to code this myself in Python inside the view?
Cheers, Victor
first question
Use the python like join filter
{{ article.company.all|join:", " }}
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#join
second question
My question is, is it better to use the dictsort template-tag to sort this inside the template, or should I use QuerySet's order_by call instead?
I would use QuerySet's order_by. I like doing such stuff in DB. Beacuse with a huge dataset you could use database indexes.
And I assume it's better to use regroup, rather than trying to code this myself in Python inside the view?
regroup. It is defintly better to use python native functions.
这篇关于Django模板 - 打印逗号分隔的ManyToManyField,将结果列表排序成dict?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!