managers: honor OneToOneField.parent_link=False (#363)

This commit is contained in:
Skia 2019-03-12 18:29:02 +01:00 committed by Asif Saif Uddin
parent 581522c46a
commit 1b9b5ac2c1
4 changed files with 17 additions and 2 deletions

View file

@ -3,6 +3,7 @@ CHANGES
master (unreleased)
-------------------
- Honor `OneToOneField.parent_link=False`.
- Fix handling of deferred attributes on Django 1.10+, fixes GH-278
- Fix `FieldTracker.has_changed()` and `FieldTracker.previous()` to return
correct responses for deferred fields.

View file

@ -158,6 +158,7 @@ class InheritanceQuerySetMixin(object):
if isinstance(rel.field, OneToOneField)
and issubclass(rel.field.model, model)
and model is not rel.field.model
and rel.parent_link
]
subclasses = []

View file

@ -73,6 +73,16 @@ class InheritanceManagerTestChild3(InheritanceManagerTestParent):
InheritanceManagerTestParent, related_name='manual_onetoone',
parent_link=True, on_delete=models.CASCADE)
class InheritanceManagerTestChild4(InheritanceManagerTestParent):
other_onetoone = models.OneToOneField(
InheritanceManagerTestParent, related_name='non_inheritance_relation',
parent_link=False, on_delete=models.CASCADE)
# The following is needed because of that Django bug:
# https://code.djangoproject.com/ticket/29998
parent_ptr = models.OneToOneField(
InheritanceManagerTestParent, related_name='child4_onetoone',
parent_link=True, on_delete=models.CASCADE)
class TimeStamp(TimeStampedModel):
pass

View file

@ -10,7 +10,8 @@ from tests.models import (
InheritanceManagerTestRelated, InheritanceManagerTestGrandChild1,
InheritanceManagerTestGrandChild1_2, InheritanceManagerTestParent,
InheritanceManagerTestChild1,
InheritanceManagerTestChild2, TimeFrame, InheritanceManagerTestChild3
InheritanceManagerTestChild2, TimeFrame, InheritanceManagerTestChild3,
InheritanceManagerTestChild4,
)
@ -141,6 +142,7 @@ class InheritanceManagerTests(TestCase):
'inheritancemanagertestchild1',
'inheritancemanagertestchild2',
'manual_onetoone', # this was set via parent_link & related_name
'child4_onetoone',
]
self.assertEqual(set(results.subclasses),
set(expected_related_names))
@ -258,7 +260,7 @@ class InheritanceManagerUsingModelsTests(TestCase):
objs = InheritanceManagerTestParent.objects.select_subclasses().order_by('pk')
objsmodels = InheritanceManagerTestParent.objects.select_subclasses(
InheritanceManagerTestChild1, InheritanceManagerTestChild2,
InheritanceManagerTestChild3,
InheritanceManagerTestChild3, InheritanceManagerTestChild4,
InheritanceManagerTestGrandChild1,
InheritanceManagerTestGrandChild1_2).order_by('pk')
self.assertEqual(set(objs.subclasses), set(objsmodels.subclasses))
@ -280,6 +282,7 @@ class InheritanceManagerUsingModelsTests(TestCase):
models = (InheritanceManagerTestChild1,
InheritanceManagerTestChild2,
InheritanceManagerTestChild3,
InheritanceManagerTestChild4,
InheritanceManagerTestGrandChild1,
InheritanceManagerTestGrandChild1_2)