Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Help with a somewhat complex reverse ForeignKey lookup. [RSS Feed]

#1 Jan. 12, 2011 14:19:26

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

Help with a somewhat complex reverse ForeignKey lookup.


If I have a couple of models like these (just an example)....

class Parent(models.Model):
name = models.CharField(max_length=100)

class Gender(models.Model):
name = models.CharField(max_length=100)

class Child(models.Model):
entity = models.ForeignKey(Entity, related_name=’children’)
gender = models.ForeignKey(Gender)
name = models.CharField(max_length=100)


How can i get a list of parents that _doesn't_ have a child of a
certain gender?

>>> gender = Gender.objects.get(name='female')
>>> Parent.objects.filter(#whatever that gets parents that doesn't have a child
>>> with gender=gender#)

I'm completely lost here so please help me out.
If it was about getting parents with no children then
"children__isnull=True" should do it but now I don't know.
The answer is probably obvious but I'm suffering from a mental block
right here.

--
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

#2 Jan. 12, 2011 14:32:38

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

Help with a somewhat complex reverse ForeignKey lookup.


On Wednesday, January 12, 2011 2:04:28 PM UTC, Peter wrote:
>
> If I have a couple of models like these (just an example)....
>
> class Parent(models.Model):
> name = models.CharField(max_length=100)
>
> class Gender(models.Model):
> name = models.CharField(max_length=100)
>
> class Child(models.Model):
> entity = models.ForeignKey(Entity, related_name=’children’)
> gender = models.ForeignKey(Gender)
> name = models.CharField(max_length=100)
>
>
> How can i get a list of parents that _doesn't_ have a child of a
> certain gender?
>
> >>> gender = Gender.objects.get(name='female')
> >>> Parent.objects.filter(#whatever that gets parents that doesn't have a
> child with gender=gender#)
>
> I'm completely lost here so please help me out.
> If it was about getting parents with no children then
> "children__isnull=True" should do it but now I don't know.
> The answer is probably obvious but I'm suffering from a mental block
> right here.
>
>
Your question is a bit confusing because the Parent model doesn't seem to be
related to anything. Should the FK to 'Entity' in Child actually be to
Parent? Assuming yes, does this give you what you want?

Parent.objects.exclude(child__gender='female')
--
DR.

--
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

#3 Jan. 12, 2011 14:34:17

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

Help with a somewhat complex reverse ForeignKey lookup.


Yes of course.... my mistake when "washing" my original messy code

Peter Magnusson

MUSTADFORS BRUKS AB | Sågbrovägen 1 | SE66010 Dals Långed | Phone +46
(0)531-71 435 | Mobile +46 (0)76-824 96 04 |http://www.mustad.seOn Wed, Jan 12, 2011 at 15:32, Daniel Roseman <dan...@roseman.org.uk> wrote:

> On Wednesday, January 12, 2011 2:04:28 PM UTC, Peter wrote:
>>
>> If I have a couple of models like these (just an example)....
>>
>> class Parent(models.Model):
>> name = models.CharField(max_length=100)
>>
>> class Gender(models.Model):
>> name = models.CharField(max_length=100)
>>
>> class Child(models.Model):
>> entity = models.ForeignKey(Entity, related_name=’children’)
>> gender = models.ForeignKey(Gender)
>> name = models.CharField(max_length=100)
>>
>>
>> How can i get a list of parents that _doesn't_ have a child of a
>> certain gender?
>>
>> >>> gender = Gender.objects.get(name='female')
>> >>> Parent.objects.filter(#whatever that gets parents that doesn't have a
>> child with gender=gender#)
>>
>> I'm completely lost here so please help me out.
>> If it was about getting parents with no children then
>> "children__isnull=True" should do it but now I don't know.
>> The answer is probably obvious but I'm suffering from a mental block
>> right here.
>>
>>
> Your question is a bit confusing because the Parent model doesn't seem to
> be related to anything. Should the FK to 'Entity' in Child actually be to
> Parent? Assuming yes, does this give you what you want?
>
> Parent.objects.exclude(child__gender='female')
> --
> DR.
>
> --
> 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<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>

--
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

#4 Jan. 12, 2011 14:36:04

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

Help with a somewhat complex reverse ForeignKey lookup.


And your example of course worked.... It's obvious really and just stupid of
me.
Thanks

--
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
  • » Help with a somewhat complex reverse ForeignKey lookup. [RSS Feed]

Board footer

Moderator control

Enjoy the 17th 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