On Tue, Aug 26, 2008 at 7:49 AM, robin <[EMAIL PROTECTED]> wrote:

>
> ###MODELS.PY###
> class Body(models.Model):
>  body=models.CharField(max_length=50,primary_key=True) #eg. sedan,
> hatchback
>  def __unicode__(self):
>    return unicode(self.body)
>
> class Car(models.Model):
>  body=models.ForeignKey(Body)
>
> ###VIEWS.PY###
> def view_car(request,object_id):
>    car=get_object_or_404(Car,id=object_id,user=request.user)


Passing in a user here doesn't make sense given the Car model you show.  It
doesn' t have a user field.


>    return render_to_response('TEMPLATE.HTML',
>                             {'car':car,
>                              'query':connection.queries,},
>                             context_instance=RequestContext(request))
>
> ###TEMPLATE.HTML###
> body = {{ car.body }}
> query = {{query}}
>
> ###The page outputs below###
>
> body = Sedan
>
> query =
> {'time': '0.003', 'sql': 'SELECT "cars_car"."id", "cars_car"."body_id"
> FROM "cars_car" WHERE ("cars_car"."id" = 1)'}
>
> {'time': '0.001', 'sql': 'SELECT "cars_body"."id", "cars_body"."body"
> FROM "cars_body" WHERE "cars_body"."id" = 1 '}
>

If you actually use the models/code (minus the attempt to include user in
the lookup) you show, this second query would be:

{'time': '0.000', 'sql': u'SELECT "cars_body"."body" FROM "cars_body" WHERE
"cars_body"."body" = Sedan '}

because the Body model would not have an id field separate from the body
field.

Which model definition you are actually using impacts how to eliminate the
2nd query.

If you want to remove 2nd query with the models you show, where body is
actually the primary key, change your template to:

body = {{ car.body_id }}
query = {{query}}

That way the foreign key id field itself, and not the referenced model, will
be printed.  That will print 'Sedan', for example, if the primary key field
is really 'body'.  But given what your 2nd query actually showed, what that
would display for the code you are really running would be an integer like
'1'.

If you want to remove the 2nd query in the case where the body field is not
the primary key, you need to include a select_related() in the original
lookup, meaning you can't use the get_object_or_404 shortcut.  You'd instead
do something like:

car = Car.objects.select_related().get(pk=object_id)

within a try/except and take care of raising the 404 yourself if you get a
DoesNotExist exception.  The select_related will change the original query
to be something like:

SELECT "cars_car"."id", "cars_car"."body_id", "cars_body"."id",
"cars_body"."body"
FROM "cars_car" INNER JOIN "cars_body" ON ("cars_car"."body_id" =
"cars_body"."id")
WHERE "cars_car"."id" = 1

Thus when you body from the template a 2nd query will not be needed.

Karen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to