Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Model 'Child' is inherited from 'Parent', trying to filter to get only Child results with Parent.objects.filter(...)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
robin  
View profile  
 More options May 31 2011, 4:53 am
From: robin <robinc...@gmail.com>
Date: Tue, 31 May 2011 01:53:26 -0700 (PDT)
Local: Tues, May 31 2011 4:53 am
Subject: Model 'Child' is inherited from 'Parent', trying to filter to get only Child results with Parent.objects.filter(...)
Example code:

   class Parent(models.Model):
        name = models.CharField(max_length='20',blank=True)

    class Child(Parent):
        pass

I want to get the result of Child.objects.all() but I DO NOT want to
use Child.
I want to use Parent instead, which I tried to do with:

    Parent.objects.filter(child__isnull=False)

Which doesn't work and gives the result of Product.objects.all()
instead.
However If I do the following, it WORKS:

    Parent.objects.filter(child__name__isnull=False)

Another way, if I insist on using
Parent.objects.filter(child__isnull=False), is to change the Child
model to the following:

    class Child(models.Model):
        parent = models.OneToOneField(Parent)

Then Parent.objects.filter(child__isnull=False) would WORK

The problem with the 2 solutions above is that filtering with
child__name__isnull looks hackish, and I don't want to change my Child
model to use OneToOneField.

So is there a better way?

Thanks,
Robin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
robin  
View profile  
 More options May 31 2011, 11:23 pm
From: robin <robinc...@gmail.com>
Date: Tue, 31 May 2011 20:23:15 -0700 (PDT)
Local: Tues, May 31 2011 11:23 pm
Subject: Re: Model 'Child' is inherited from 'Parent', trying to filter to get only Child results with Parent.objects.filter(...)
Shall I report the inability of using
Parent.objects.filter(child__isnull=False)  as a bug?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matías Aguirre  
View profile  
 More options May 31 2011, 11:40 pm
From: Matías Aguirre <matiasagui...@gmail.com>
Date: Wed, 01 Jun 2011 00:40:05 -0300
Local: Tues, May 31 2011 11:40 pm
Subject: Re: Model 'Child' is inherited from 'Parent', trying to filter to get only Child results with Parent.objects.filter(...)
This worked on on Django 1.2.5:

    Parent.objects.exclude(child__isnull=True)

Does it work for you?

Excerpts from robin's message of Tue May 31 05:53:26 -0300 2011:

--
Matías Aguirre <matiasagui...@gmail.com>

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
robin  
View profile  
 More options Jun 1 2011, 5:07 am
From: robin <robinc...@gmail.com>
Date: Wed, 1 Jun 2011 02:07:50 -0700 (PDT)
Local: Wed, Jun 1 2011 5:07 am
Subject: Re: Model 'Child' is inherited from 'Parent', trying to filter to get only Child results with Parent.objects.filter(...)
Yes that works, thank you.
I've submitted a bug report here https://code.djangoproject.com/ticket/16135
The bug is that Parent.objects.filter(child__isnull=False) is not
giving the result I expect.

On Jun 1, 10:40 am, Matías Aguirre <matiasagui...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »