Recombining multiple inheritance in Django models

826 Views Asked by At

Been using Django Polymorphic to help me with complex inheritance patterns in my Django model.

I'm having an issue where I need to have a concrete base model (because it needs to have a ForeignKey to itself), and then have multiple models inherit from it, with eventually some model inheriting from multiple of those. The issue is that I'm getting a E005 error:

app.NamedWeightedModel: (models.E005) The field 'basemodel_ptr' from parent model 'app.namedmodel' clashes with the field 'basemodel_ptr' from parent model 'app.massmodel'.

The simple example that triggers this is shown below:

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


class BaseModel(PolymorphicModel):
    parent = models.ForeignKey('self')


class NamedModel(BaseModel):
    name = models.CharField(max_length=32)


class MassModel(BaseModel):
    weight = models.FloatField()


class NamedWeightedModel(NamedModel, MassModel):
    pass

Is there a better way to handle this? Or a standard approach?

My real model is much more complex (an implementation of the UML spec), and I would like to be able to use multiple inheritance (ideally through Polymorphic) to make things easier.

I've considered making any multiple inheritance models be single inheritance with all additional inherited models mapped as OneToOneFields, but this negates some of the nice things that polymorphic does.

Any help you can provide would be greatly appreciated.

0

There are 0 best solutions below