Auth.User.None when rendering a ManyToManyField

|▌冷眼眸甩不掉的悲伤 提交于 2020-02-05 06:36:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!