There are the following models:

class ARM (models.Model): class Meta(): db_table = 'arm' network = models.ManyToManyField(Network) arm_configuration_name = models.TextField() class Network(models.Model): class Meta(): db_table = 'network' network_name = models.TextField() 

In the template, pass it like this (views.py)

 def Display_example(request): arms = ARM.objects.all() rooms = Room.objects.all() network = Network.objects.all() server = Server.objects.all() return render(request, 'example.html', {'arms': arms ' network': network}) 

It is necessary to display all the information about the AWP in the template (including and which network it belongs to). I try this:

 {% for arm in arms %} <div> <p>{{ arm.arm_configuration_name }}</p> {% for network in networks %} {% if network.id == arm.network %} <p>{{ network.network_name }}</p> {% endif %} {% endfor %} </div> {% endfor %} 

But information about which network the AWP belongs to is not displayed. Do not quite understand why.

  • Are you sure that you need a network field of exactly ManyToMany type? - Nikmoon

1 answer 1

I think you are trying to compare 2 fields of different types in the string {% if network.id == arm.network %} , the first is most likely an integer, the second is an instance of Network.

I think it would be better to change the code a bit:

 {% for network in arm.network.all %} <p>{{ network.network_name }}</p> {% endfor %} 

Here it is described in great detail, clearly and with examples, how to work with fields like ManyToMany.

  • Thanks, really helped. Be sure to read the documentation. - Evgeny Vasilyev