Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Foreign Key lookups in Both Directions [RSS Feed]

#1 Nov. 3, 2005 20:41:53

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

Foreign Key lookups in Both Directions


In the many to one relationship example
(http://www.djangoproject.com/documentation/models/many_to_one/) the
Article model defines a foreign key relationship with the Reporter
model. One commenter asks if it would be possible to do a lookup like
this:

reporters.get_list(article__pub_date__exact=datetime(2005, 8, 5))

to get a list of reporters who published articles on a specific date.

Their question is unanswered, so I'm asking it here because I'd like to
do something similar, but when I do I get an error.

My models:

class Thing(meta.Model):
name = meta.CharField(maxlength=100)

class Person(meta.Model):
zipcode = meta.CharField(blank=True, maxlength=5)
nickname = meta.CharField(maxlength=100)

class Connection(meta.Model):
blog = meta.ForeignKey(Thing)
owner = meta.ForeignKey(Person, edit_inline=meta.TABULAR,
core=False)

>>>connections.get_list(owner__zipcode__exact='98109') works for me

>>>things.get_list(connection__owner__zipcode__exact='98109') fails
as does
things.get_list(connection__pk=1) which gives the error "TypeError: got
unexpected keyword argument 'connection__pk'"

Am I missing something obvious?

Thanks.

Offline

#2 Nov. 3, 2005 21:01:36

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Foreign Key lookups in Both Directions


On 11/3/05, eas <> wrote:
> class Thing(meta.Model):
> name = meta.CharField(maxlength=100)
>
> class Person(meta.Model):
> zipcode = meta.CharField(blank=True, maxlength=5)
> nickname = meta.CharField(maxlength=100)
>
> class Connection(meta.Model):
> blog = meta.ForeignKey(Thing)
> owner = meta.ForeignKey(Person, edit_inline=meta.TABULAR,
> core=False)
>
> >>>things.get_list(connection__owner__zipcode__exact='98109') fails
> as does
> things.get_list(connection__pk=1) which gives the error "TypeError: got
> unexpected keyword argument 'connection__pk'"

things.get_list(connection__pk=1) isn't a valid use of the API. The
only dynamic parameter you can pass to things.get_list() would start
with "name" -- e.g. things.get_list(name__contains='foo').

To accomplish what you want to do, try this:

>>> connection_list =
connections.get_list(owner__zipcode__exact='98109',
select_related=True)

>>> things.get_list(id__in=)

This is two queries instead of one, of course. If you're concerned
with efficiency, you can write the SQL manually. See the example here:http://www.djangoproject.com/documentation/models/custom_methods/Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

  • Root
  • » Django
  • » Foreign Key lookups in Both Directions [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of November
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