jazzband/django-model-utils

InheritanceManager not resolving subclasses with django.test.utils.isolate_apps

DayDotMe opened this issue · 0 comments

Problem

I'm running a TestCase with isolate_apps as context manager. In this test case, I defined two dummy models which inherits a global model BaseModel which uses InheritanceManager. When I try to resolve subclasses using select_subclasses, it returns instances of BaseModel instead of subclasses.

If i remove isolate_apps context manager it works fine but other test cases fail because foo and bar tables don't exist.

Environment

  • Django Model Utils version: 4.3.1
  • Django version: 4.2
  • Python version: 3.9.7

Code examples

with isolate_apps("app_label"):

    class FooModel(BaseModel):
        name = models.CharField(default="foo")

    class BarModel(BaseModel):
        name = models.CharField(default="bar")

    class MyTestCase(TestCase):
        @staticmethod
        def setup_foobar_models():
            with connection.schema_editor() as schema_editor:
                schema_editor.create_model(FooModel)
                schema_editor.create_model(BarModel)
                
        @classmethod
        def setUpClass(cls) -> None:
            super().setUpClass()
            cls.setup_foobar_models()
            
        def test_foo(self):
            foo = FooModel.objects.create()
            res = BaseModel.objects.filter(id=foo.id).select_subclasses().get()
            self.assertTrue(isinstance(res, FooModel))