How do you check if the current user is the same as a foreign key in a model

0 votes

I have a model called Tribe which contains a foreign key (user) called chieftain. I made a method in the model which checks if the current user is the same as the foreign key. But when I try to use the method in my html code it doesn't work, no errors show up it just doesn't show a link its supposed to even though the foreign key is set to the user I've logged in. 

Here's the code

tribe.html

<div class="row">
    <div class="col-md-10">
            {% if tribe.is_chieftan %}
                <p><a href="{% url 'app:update-tribe' %}">Update tribe</a></p>
            {%endif%}
    </div>
</div>

model.py

class Tribe(TimeStamped):
    name = models.CharField(max_length=200,unique=True)
    chieftain = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    tribe_members = models.ManyToManyField(Member)

    def __str__(self):
        return self.name[:80]

    def is_chieftain(self,request):
        if (self.chieftain == request.user):
            return True
        else:
            return False

Jan 26, 2021 in Python by Andrija

edited Mar 4, 2025 287 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP