Model 'Child' is inherited from 'Parent', expecting to get only Child results with Parent.objects.filter(child__isnull=False), but does not work.
|Reported by:||Robin||Owned by:|
|Component:||Database layer (models, ORM)||Version:||1.3|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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
I want to use Parent instead, which I tried to do with:
Parent.objects.filter(child__isnull=False) # not work, but should
Which doesn't work and gives the result of Product.objects.all() instead.
However If I do the following, it works:
Parent.objects.exclude(child__isnull=True) # work
If I insist on using:
Parent.objects.filter(child__isnull=False) # will work if do below
Then it will work if I use the OneToOneField for Child.
class Child(models.Model): parent = models.OneToOneField(Parent)
I expect the query to also work with Model Inheritance as well.
Change History (5)
comment:1 Changed 5 years ago by
|Patch needs improvement:||unset|
|Triage Stage:||Unreviewed → Accepted|
comment:2 Changed 5 years ago by
|Owner:||changed from nobody to Nikolay Zakharov|
|Status:||new → assigned|