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

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

Open
DayDotMe opened this issue Apr 27, 2023 · 0 comments

Comments

@DayDotMe
Copy link

DayDotMe commented Apr 27, 2023

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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant