Skip to content

Alternative to migrate to polymorphic models in legacy projects: double inheritance. Is it possible? #273

@jorgeas80

Description

@jorgeas80

Here, my use case:

from django.db import models
from polymorphic.models import PolymorphicModel

class BaseClassWithLotsOfStuff(models.Model):
    # Here, a lot of existing atributes (variables and methods)
    # ...

class InheritedClassOne(BaseClassWithLotsOfStuff):
    # A lot of stuff here too...
    #...

Those classes are part of a legacy project, and I can't change them to PolymorphicModel (too many possible side effects). But now, I need to extend the project, by inheriting more classes from the base class. And I want to use django-polymorphic.

So, my point: why don't I create a new base class, inherit that class from the original BaseClassWithLotsOfStuff (to allow using the goodies it contains), make it polymorphic and inherit the rest from it?

class NewBaseClass(BaseClassWithLotsOfStuff, PolymorphicModel):
    # New stuff here
    # ...


class InheritedClassTwo(NewBaseClass):
    # We are polymorphic now!
    # ...

So I tried that. And when running makemigrations, I got this error

AssertionError: PolymorphicModel: "InheritedClassTwo._default_manager" manager is of type "Manager", but must be a subclass of PolymorphicManager

I guess the problem is PolymorphicModel defines a custom manager, amongs other things, and it crashes because of the double inheritance in BaseNewClass.

So, does my point make sense? Can it be done in a different way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions