Fatur Ewing
Fatur Ewing

Reputation: 33

Django ManytoManyField Display User Profile

I got stuck when trying to display User Name as Manytomanyfield in django template, but always display with

<QuerySet [<Profile: User 1>]>

how can i display it without queryset bla bla bla... just User 1 (First Name or Last Name or Full Name)

class Referral(models.Model):
    ref = models.ManyToManyField(Profile, related_name='ref')
    profile = models.ForeignKey(Profile, on_delete=models.PROTECT)

    def __str__(self):
        return str(self.ref)
{% for tree in tree %}
<section>
<span class="diagram-icon"></span>
<span class=diagram-label>{{ tree.ref.all }}</span>
</section>
{% endfor %}

Upvotes: 0

Views: 100

Answers (2)

Shahid Tariq
Shahid Tariq

Reputation: 931

If you want to print all the usernames of particular referral, you can do it like:

{% for r in referral.ref.all %}
        {{ r.username}}
{% endfor %}

Upvotes: 0

Arpit
Arpit

Reputation: 12797

Try using

<span class=diagram-label>{{ tree.ref.all.0.firstName }}</span>

If you want all users, you can do:

{% for ref_user in tree.ref.all %}
    {{ ref_user }}
{% endfor %}

Upvotes: 3

Related Questions