Skip to content

Commit

Permalink
fix: Use proper column name instead of attname (#573)
Browse files Browse the repository at this point in the history
fix: Use proper column name instead of attname
  • Loading branch information
last-partizan authored Jun 16, 2023
1 parent 6916342 commit 0ec4ac5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ To be released
- Add support for `Python 3.12` (GH-#545)
- Drop support for `Python 3.7` (GH-#545)
- Swedish translation (GH-#561)
- Use proper column name instead of attname (GH-#573)

4.3.1 (2022-11-15)
------------------
Expand Down
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

0 comments on commit 0ec4ac5

Please sign in to comment.