There are two models from different applications, such as:

model1.py

class SomeClass1(models.Model): short_name = models.CharField(...) compatibility = models.ManyToManyField(SomeClass2, ....) 

model2.py

 class SomeClass2(models.Model): name_dev = models.CharField(...) 

Actually, when I create a model1 object, I can select related objects from model2 . And in the template I can {{object.compatibility.name_dev}} type - {{object.compatibility.name_dev}}

But also in the template I would like to get all the objects that referred to the associated object from model2

It is possible that something like this:

views2.py

 object2 = SomeClass2.objects.all() 

In the template:

{% for object in object2 %}

 `{{object.(...)compatibility.short_name}}` 

{% endfor %}

You can add a field to the second model, but this will again have to manually select objects from model1 . This is what I would like to avoid.

    1 answer 1

    Django automatically adds fields for manytomany.

    In your case, such filters are automatically available:

     SomeClass1.objects.filter(someclass2__name_dev = u'Sidorov') 
    • In general, yes. Only I changed the design a bit and it became what was needed: SomeClass1.objects.filter(compatibility__name_dev = u'Sidorov') Your option displayed a couple of objects that I did not understand. Thank you - Vlad Zhdanov
    • Displays objects corresponding to SomeClass1, which are referenced by the name_dev filter in SomeClass1. In general, there are a lot of any available. I use pycharm, it has autocomplete - it offers a bunch of keys for ManyToMany. If the answer helped - put it as a solution :) - Mikhail Alekseevich