问题
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")
(....)
Thanks
Edit ;
I am able to rend users but I have an unesthetic code being render : .
I would like to render only the username.
回答1:
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.
来源:https://stackoverflow.com/questions/54953783/auth-user-none-when-rendering-a-manytomanyfield