django - Select a single field from a foreign key -


so have a simple (simplified) model

class myobject(models.model):     owning_user = models.foreignkey(user, null=true) 

now in 1 of templates i'm trying iterate on list of these objects determine whether should displayed similar this

{% my_object in foo.my_object_set %}     {% if my_object.owning_user.id == user.id %}          show me!     {% endif %} 

this works fine, finding query

my_object.owning_user.id 

returns every field owning user before getting id verified both in django debug tool bar, , checking connection queries

# django-debug-toolbar states repeated multiple times      select ••• "auth_user" "auth_user"."id" = 1  # following test code confirms django.db import connection conn = connection bearing_type.owning_user.id print conn.queries[-1] 

now since query repeats on 1000 times , takes 2ms per query taking 2 seconds perform - when care id...

is there anyway @ can perform query id owning_user instead of having query fields?

note, i'm trying hard here avoid making raw query

if use my_object.owning_user_id instead of my_object.owning_user, django use id instead of looking user.

in case, need id, if needed other user attributes, use select_related. in view, do:

foo.my_object_set.select_related('user') 

in template, don't have control, since can't pass arguments.

{{ foo.my_object_set.select_related }} 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -