开发者

Filtering many-to-many relationships in django templates

开发者 https://www.devze.com 2023-04-13 05:06 出处:网络
Another likely easy answer, but if I have a data structure called x.y that represents a many-to-many relationship, is there an easy way -- within the context of a django template -- to query x.开发者_

Another likely easy answer, but if I have a data structure called x.y that represents a many-to-many relationship, is there an easy way -- within the context of a django template -- to query x.开发者_JAVA百科y.all (or whatever) for a specific field, or do I need to set up a for loop?


I'm afraid that there's not really a better way to do it. You could write a custom template tag if it was really arduous or something, but this is probably what you're looking for.

{% for related in instance.some_related.all %}{{ related.field }}{% endfor %}


Have you tried this?:

object.many_to_many_field.select_related().filter(field=something)

EDIT: Sorry, I didn't understand. Like the other answer, I have found no way to do this in a template since you can't pass any argument.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号