Wednesday, June 1, 2011

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:
> 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:
>
>
>
> > 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
>
> --
> Matías Aguirre <matiasagui...@gmail.com>

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment