[Solved]-Using ABC, PolymorphicModel, django-models gives metaclass conflict

2đź‘Ť

âś…

If you are using Python 3, you are trying to use your derived metaclass incorrectly.

And since you get “the same error”, and not other possible, more subtle, error, I’d say this is what is happening.

Try just changing to:

class IntermediaryMeta(type(InterfaceToTransactions), type(PolymorphicModel)):
    pass

class Category(PolymorphicModel, InterfaceToTransactions, metaclass=IntermediaryMeta):
    ...

(At least the ABCMeta class is guaranteed to work collaboratively using super, that is enough motive to place the classe it first on the bases )
tuple)

If that yields you new and improved errors, this means that one or both of those classes can’t really collaborate properly due to one of several motives. Then, the way to go is to force your inheritance tree that depends on ABCMeta not to do so, since its role is almost aesthetical in a language where everything else is for “consenting adults” like Python.

Unfortunatelly, the way to that is to use varying methods of brute-force, from safe “rewritting everything” to monkey patching ABCMeta and abstractmethod on the place were “InterfaceToTransactions” is defined to simply do nothing.

If you need to get there, and need some help, please post another question.

Sorry – this is actually the major drawbacks of using metaclasses.

👤jsbueno

1đź‘Ť

Unless django-polymorphic decides to inherit from abc.ABC this is going to be very difficult to achieve. A good solution would be to "manually" create your interface. For instance:

class InterfaceToTransactions:
    def account(self):
        raise NotImplementedError("Account method must be implemented.")
    ...

class Category(PolymorphicModel, InterfaceToTransactions):
    def account(self):
        return self.source_account
    ...

class Income(TimeStampedModel, InterfaceToTransactions):
    def account(self):
        return self.destination_account
    ...
👤YPCrumble

Leave a comment