Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:15:58+00:00 2026-06-03T11:15:58+00:00

I have a model called UserProfile defined as class UserProfile(models.Model): user = models.OneToOneField(User, related_name=’userprofile_from_user’)

  • 0

I have a model called UserProfile defined as

class UserProfile(models.Model):
    user       = models.OneToOneField(User, related_name='userprofile_from_user')
    user_types = models.ManyToManyField(UserType, related_name='userprofiles_from_user_types', null=True, blank=True)

    def has_user_types(self, user_types):
        return self.user_types.filter(name__in=user_types).count()

UserType is defined as

class UserType(models.Model):
    TYPE_CHOICES = (
        ('ad', 'administrator'   ), # 1
        ('mo', 'moderator'       ), # 2
        ('vi', 'viewer'          ), # 3
        ('pm', 'property manager'), # 4
        ('po', 'property owner'  ), # 5
        ('vm', 'vendor manager'  ), # 6
        ('ve', 'vendor'          ), # 7
        ('te', 'tenant'          ), # 8
    )

    name = models.CharField(max_length=2, choices=TYPE_CHOICES)

I want to be able to use the UserProfile‘s method has_user_types(). In a view, I would do something like

if user.profile.has_user_types(['ad', 'mo', 'pm']):
    # The user is any combination of an administrator, moderator, or property manager.

But can I do the same thing in the template? I am specifically checking for a few user types so I wanted to do something like

{% if user.profile.has_user_types(['te']) %}

I know I could simply define another method called is_tenant() in the model (that takes no argument), but I also wanted to check other user types and I was wondering if I could consolidate has_user_types().

Side question: If Django’s default template can’t do it, then can Jinja2 do it?


Solution

Thanks to Ignacio Vazquez-Abrams’s help!

custom_tags.py:

@register.assignment_tag
def has_user_types(user_pk, *args):
    user = User.objects.get(pk=user_pk)

    return user.profile.has_user_types(args)

template:

{% load has_user_types from custom_tags %}

{# I pass the pk because I want to be able to pass any user, not just request.user #}
{% has_user_types user.pk "te" as is_tenant %}
{% if is_tenant %}
    {# Show something #}
{% endif %}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:16:00+00:00Added an answer on June 3, 2026 at 11:16 am

    No. Write a custom filter or template tag that checks for them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class UserProfile(models.Model): user = models.OneToOneField(User) score = models.PositiveIntegerField() class
I have an extended UserProfile model in django: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True)
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user
In my application I have an extended User model called UserProfile. This user can
I have a model called Question and another one called Answer. class Question(models.Model): text
I have a model as follows class UserPrivacy(models.Model): user = models.ForeignKey(User) profile = models.SmallIntegerField(default=1,
I have a model called Contact which has_one guest like so. class Contact <
I have a model called company that has_many users then users belongs_to company. class
I have a model called user and another model called student. Neither of them
I have a model called Customer in google app engine with python: class Customer(db.Model):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.