M. Gar
M. Gar

Reputation: 887

Access to models with a foreign key in the template

I have a profile model with a one-to-one relationship to the User model so I can access to both models in the templates tanks to the user variable like this:

template.html

{% if user.profile.phone == 1234567890 %}
    Show something
{% endif %}

That works fine, the condition gives True and show something but I have too the models Property and User_Property, the User_Property model have as Foreignkey the ids from User and Property.

models.py

class Property(models.Model):
    name = models.CharField(max_length=50, unique=True)

class User_Property(models.Model):
    us = models.ForeignKey(User, related_name='up_us')
    prop = models.ForeignKey(Property, related_name='up_prop')

So if I try to access to the User_Property model like this:

{% if user.user_property.prop == 1 %}
    Show something
{% endif %}

I can't access it shows nothing like it was False even when it's True, I have tried with user.user_property.prop_id == 1 too. It is beacause the relationship with the Profile model was made with the OneToOneField and the relationship with User_Property was made with the ForeignKey field and I need to pass in the context the User_Property model?

And it is possible to access to Property model like if I use a JOIN SQL statement in the template? something like this:

{% if user.user_property.property.name == 'the name of the property' %}
    Show something
{% endif %}

Sorry for the long Post but I tried to add all the need info.

EDIT: Ok if someone need something similar this is what I did to solve the problem.

Create a context_processor.py to return a instance of User_Property and add it to my settings.py in this way I can access to the instance in all my templates even if I don't pass it as context in the views.

context_processors.py

from App_name.models import User_Property
from django.contrib.auth.models import User

def access_prop(request):
    user = request.user.id #add the .id to no have problems with the AnonymousUser in my logg-in page
    user_property = User_Property.objects.filter(us=user).values_list('prop', flat=True) #this stores the list of all the properties for the logg-in user
    return {
        'user_property': user_property,
    }

settings.py

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS += ('App_name.context_processors.access_prop',)

Then in the template check if the user have a especific property

template.html

{% if 'property name' in user_property %}
    Show something
{% else %}
    This is not for you
{% endif %}

To can check in especific for the name instead of the id just add to_field='name' in my prop field in the model User_Property like this: prop = models.ForeignKey(Property, related_name='up_prop', to_field='name').

Upvotes: 0

Views: 2526

Answers (3)

arcegk
arcegk

Reputation: 1480

From the docs

You should use the related_name that you set in the ForeignKey and the built-in methods of the relationships:

try this:

user.up_us.filter(prop__name="the name")

EDIT

for using the .filter(prop__name="the name") method you have to do it in a .py file.

Upvotes: 1

vishes_shell
vishes_shell

Reputation: 23484

You've set related_name in us = models.ForeignKey(User, related_name='up_us'), so you need to use it

{% if user.up_us.prop.name == 'the name of the property' %}
    Show something
{% endif %}

This answer has a good explanation of how to use and what related_name for.

And try to exclude to much logic from templates.

Upvotes: 1

Gocht
Gocht

Reputation: 10256

Give this a try: {% if user.user_property.prop.id == 1 %}

Upvotes: 1

Related Questions