How Do I Loop Through Two Different ManyToMany Quersyets in Python Django?

Viewed 23

I am trying to do something seemingly simple and it won't cooperate. I am trying to use the django template language to loop through a model. Easy enough. The challenge here is that the model contains two manytomany fields that I am trying to compare.

In my model...

owner_confirm = models.ManyToManyField(User,related_name='meeting_minutes_action_item_owner_confirm')
owner = models.ManyToManyField(User)

Simple enough...

I have a model that populates these two fields...and that's all working. The problem is when I loop through the template...it's not recognizing them when I try to compare them doing something like...

  {% if action_item.owner.all == action_item.owner_confirm.all %}

What am I doing incorrectly? I have been at this for 2 days and am beginning to lose my mind. The condition above passes as false even if it's true...when I loop through all other types of fields it works fine...manytomany is not.

1 Answers

With ManytoMany relationship, you usually have to call them in a different way.

Try something like {% if action_item.owner_set.all == actions_item.owner_confirm_set.all %}

I don't remember the exact reason, but many to many relationships are represented by a set and must be called in this way

Related