###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)
    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 '}

------end example--------
Body is simply acting like a choicebox, so that Car table could have a
dynamic restriction on what goes into its body column.

As you can see there are 2 queries that execute in th end. First
queries from the Car table. Second queries from the Body table.

Now what I wanna do is to get rid of the second query because
Body.body is set manually as a varchar primary key, so the first query
would have gotten me the value of 'Sedan' from the Car table already.

If Body.body was not a primary key, then yes the last query would have
been necessary, because the first query would have returned the
integer value of the primary key. Then we need the second query to
grab the varchar value ('Sedan') of the integer key.

So is there a way to allow a particular foreign key (from Car) to
follow on to the related table/object (Body), while another foreign
key (also from Car) is not allowed to follow and just take the value
that's already in the current table?

Am I making sense?
I know what I'm doing is quite pedantic.

Thanks a lot.
--~--~---------~--~----~------------~-------~--~----~
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