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 (4)
comment:1 Changed 3 years ago by aaugustin
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted
comment:2 Changed 3 years ago by desh
- Owner changed from nobody to desh
- Status changed from new to assigned
- UI/UX unset