Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use proper column name instead of attname #573

Merged
merged 4 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion model_utils/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def instance_of(self, *models):
where_queries.append('(' + ' AND '.join([
'"{}"."{}" IS NOT NULL'.format(
model._meta.db_table,
field.attname, # Should this be something else?
field.column,
) for field in model._meta.parents.values()
]) + ')')

Expand Down
6 changes: 6 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ class InheritanceManagerTestChild3(InheritanceManagerTestParent):
parent_link=True, on_delete=models.CASCADE)


class InheritanceManagerTestChild3_1(InheritanceManagerTestParent):
parent_ptr = models.OneToOneField(
InheritanceManagerTestParent, db_column="custom_parent_ptr",
parent_link=True, on_delete=models.CASCADE)


class InheritanceManagerTestChild4(InheritanceManagerTestParent):
other_onetoone = models.OneToOneField(
InheritanceManagerTestParent, related_name='non_inheritance_relation',
Expand Down
13 changes: 12 additions & 1 deletion tests/test_managers/test_inheritance_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
InheritanceManagerTestChild1,
InheritanceManagerTestChild2,
InheritanceManagerTestChild3,
InheritanceManagerTestChild3_1,
InheritanceManagerTestChild4,
InheritanceManagerTestGrandChild1,
InheritanceManagerTestGrandChild1_2,
Expand Down Expand Up @@ -141,6 +142,7 @@ def test_manually_specifying_parent_fk_including_grandchildren(self):
'inheritancemanagertestchild1',
'inheritancemanagertestchild2',
'manual_onetoone', # this was set via parent_link & related_name
'inheritancemanagertestchild3_1',
'child4_onetoone',
]
self.assertEqual(set(results.subclasses),
Expand Down Expand Up @@ -256,7 +258,9 @@ def test_selecting_all_subclasses_specifically_grandchildren(self):
objs = InheritanceManagerTestParent.objects.select_subclasses().order_by('pk')
objsmodels = InheritanceManagerTestParent.objects.select_subclasses(
InheritanceManagerTestChild1, InheritanceManagerTestChild2,
InheritanceManagerTestChild3, InheritanceManagerTestChild4,
InheritanceManagerTestChild3,
InheritanceManagerTestChild3_1,
InheritanceManagerTestChild4,
InheritanceManagerTestGrandChild1,
InheritanceManagerTestGrandChild1_2).order_by('pk')
self.assertEqual(set(objs.subclasses), set(objsmodels.subclasses))
Expand All @@ -278,6 +282,7 @@ def test_selecting_all_subclasses_specifically_children(self):
models = (InheritanceManagerTestChild1,
InheritanceManagerTestChild2,
InheritanceManagerTestChild3,
InheritanceManagerTestChild3_1,
InheritanceManagerTestChild4,
InheritanceManagerTestGrandChild1,
InheritanceManagerTestGrandChild1_2)
Expand Down Expand Up @@ -426,6 +431,12 @@ def test_limit_to_specific_subclass(self):

self.assertEqual([child3], list(results))

def test_limit_to_specific_subclass_with_custom_db_column(self):
item = InheritanceManagerTestChild3_1.objects.create()
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild3_1)

self.assertEqual([item], list(results))

def test_limit_to_specific_grandchild_class(self):
grandchild1 = InheritanceManagerTestGrandChild1.objects.get()
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestGrandChild1)
Expand Down