Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 1, 2005 20:01:54

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

foreign key lookup problem


I have difficulties using field lookups on foreign keys. I'm trying to
retrieve a list of items with a specific state using the following
model:

class State(meta.Model):
state = meta.CharField(maxlength = 12, primary_key = True)

class Item(meta.Model):
wdate = meta.DateField()
state = meta.ForeignKey(State)

% python
>>> from django.models.ttime import *
>>> items.get_list(state__id__in = )

TypeError: got unexpected keyword argument 'state__id__in'

I've read the "Field lookups" section of the "Database API reference",
and my understanding is that the field key should be available. I've
also tried "state__in", "state_id__in".

Anyone have a clue?
/LarS

Offline

#2 Nov. 1, 2005 20:06:41

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

foreign key lookup problem


On 11/1/05, stava <> wrote:
> class State(meta.Model):
> state = meta.CharField(maxlength = 12, primary_key = True)
>
> class Item(meta.Model):
> wdate = meta.DateField()
> state = meta.ForeignKey(State)
>
> % python
> >>> from django.models.ttime import *
> >>> items.get_list(state__id__in = )
>
> TypeError: got unexpected keyword argument 'state__id__in'
>
> I've read the "Field lookups" section of the "Database API reference",
> and my understanding is that the field key should be available. I've
> also tried "state__in", "state_id__in".

"state__id" isn't working because none of your fields are called "id".
The database API uses the field names. In your case, the field name is
"state", so here's what you'd want:

items.get_list(state__state__in=)

The first "state" refers to the "state" field in Item. The second
refers to the "state" field in State.

Adrian

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

Offline

#3 Nov. 1, 2005 20:15:58

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

foreign key lookup problem


God you're quick, I just posted this.

Just tested it and it works fine now.

Another alternative I tried out in the meantime was to change the
definition of State taking out the "primary_key = True". That worked
fine as well.

Thanks a bunch!
/LarS

Offline

#4 Nov. 1, 2005 20:36:16

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

foreign key lookup problem


On 11/1/05, stava <> wrote:
> Another alternative I tried out in the meantime was to change the
> definition of State taking out the "primary_key = True". That worked
> fine as well.

Yes, that would work. Just for your knowledge, the reason that would
also work is that removing "primary_key = True" in the State model
implicitly creates an "id" field. In that case, state__id__in would be
a valid lookup parameter.

Adrian

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

Offline

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