Filtering many-to-many relationships in django templates
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.
精彩评论