Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (19.9k points)

I am trying to render a ManyToManyField from a model that render a list of hotels with staff that belong to each hotel.

I am trying to display in the template the users that are inside the current hotel being shown but I get an error

auth.User.None

my template

{% for Hotel in object_list %}

         {{ Hotel.collaborateurs }}

              {% endfor %}

My models.py

class Hotel(models.Model):

collaborateurs = models.ManyToManyField(User, verbose_name="Liste des collaborateurs autorisés")

              (....)

I am able to rend users but I have an unesthetic code being render : .

I would like to render only the username.

1 Answer

0 votes
by (25.1k points)

You need to use .all as manytomany relations are always lazy loaded in django.

Hotel.collaborateurs.all

Moreover variable names should be lowercase in Python.

collaborateurs = need an indent on the left side.

Hope that helps.

Browse Categories

...