Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Utility of select_related when rendering ForeignKeys in a form? [Developer eyes appreciated] [RSS Feed]

#1 Dec. 13, 2010 23:09:29

Margie R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Utility of select_related when rendering ForeignKeys in a form? [Developer eyes appreciated]


Could someone with some developer background take a look at my
question below and let me know if my understanding is correct?

Let's say I have a Task model that contains a foreign key to a User:

class Task(models.Model)
owner = models.ForeignKey('auth.User')

Now let's say I have a ModelForm that contains this owner field. When
I render that form for an instance of a task, the django method
models_to_dict() is called to get the initial values for the task.
For my owner field, it sets the initial value to the numeric id of the
user.

This seems to negate any utility of select_related() when I am later
rendering my form. For example, if my task is retrieved through a
query and I have select_related set to 1, that query will do an extra
lookup so that the owner's first name and last name are available with
no additional sql query, assuming I am accessing them from the
instance that the intial query returned. However, when I am rendering
my form, the value passed in is just the id of the user, ie '1'. So
my widget must now do a new lookup to get the actual user in order to
print the first name and last name.

Can anyone comment on whether my understanding is correct? Should I
simply not be using select_related in this situation? Is it primarily
intended for use from views.py, where you have more control over
exactly how and when you reference the attributes of a model
instance? Or is there some better way I could be making use of it?

Thank you!

Margie


--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Utility of select_related when rendering ForeignKeys in a form? [Developer eyes appreciated] [RSS Feed]

Board footer

Moderator control

Enjoy the 23rd of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support