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 %}
No. Write a custom filter or template tag that checks for them.