Thursday, February 27, 2020

Re: Many to Many relationships in template


Missatge de Robb Rodirguez Jr. <bertrodriguez1993@gmail.com> del dia dv., 28 de febr. 2020 a les 1:14:
Good day guys, im new to django im having in many to many relationship display in template

How can display this into a normal list..

from . 
<QuerySet [<User: webadmin>, <User: kim>, <User: sem>, <User: quinito>, <User: user1>]>

to.
webadmin
kim
sem
quinito
user1

Here's my code

model.py 
class ListOfUser(models.Model):
users = models.ManyToManyField(User, verbose_name='List of User')


views.py
def listofusers(request):
userlist = ListOfUser.objects.get(id=1)

form = ListofUserForms()

context = {
'form': form,
'userlist': userlist
}
return render(request, 'listofusers.html', context)

template
<p>{{userlist.users.all}}<p>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f3e604a7-5613-4f12-8aa6-526b19ae0fc7%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK-JoTQQr%2BQODZoqWfJLNcdFsrkvLWXmUOqES0wcxSfCSHO59A%40mail.gmail.com.

No comments:

Post a Comment