0

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.y.all (or whatever) for a specific field, or do I need to set up a for loop?

2 Answers 2

2

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 %}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.