user7724264
user7724264

Reputation:

Django Template Foreign Key Check

In model:

class Example
    text

class Share
    example (foreign_key)
    user    (foreign_key)

In view:

def page:
    items = Example.objects.get(user=user)
    user_shares = Example.objects.get(user=user)
    return render(page.html, {'items': items, 'user_shares': user_shares})

In the template I can show items in rows. But for the shared ones I want to put for example additional buttons. How can I use something like {% if item in shares %} in for loop? Or do you have better ideas?

Upvotes: 2

Views: 1640

Answers (1)

alfonso.kim
alfonso.kim

Reputation: 2900

in the template:

{% for item in items %}
<td>{{item.text}}</td>
{%if item.shares.count > 0 %}
<td><!-- additional buttons here --></td>
{% endif %}
{% endfor %}

you need to modify the ForeignKey in the Example model:

class Share(models.Model):
    example = models.ForeignKey(Example, related_name='shares')
    ...

Hope this helps.

Upvotes: 4

Related Questions