neo4django multiple inheritance

2019-07-26 17:39发布

问题:

I was trying to create my model MyUser extending neo4django.auth.models.User, so I can use the underlying authentication system. The problem is I want create also a superclass from which derive many methods and attributes that are very common for my different kind of nodes.

I did this:

from neo4django.auth.models import User as AuthUser
class MyBaseModel(models.NodeModel):
    ....
    class Meta:
        abstract = True

class MyUser(MyBaseModel,AuthUser):
    ...

but any operation on the model gives me
ValueError: Multiple inheritance of NodeModels is not currently supported.

Suggestions, workarounds?
Since MyBaseModel is essentially a container of methods and attributes, maybe a decorator that adds that fields would be an elegant solution?

Thanks.

回答1:

You're right- Multiple inheritance with multiple NodeModel-inheriting bases won't work.

However, could MyBaseModel inherit from AuthUser? If not, you can also mixin a non-NodeModel class. So if MyBaseModel is just a container for methods, you can just do

class MyBaseModelMixin(object):
    ....

and then inherit from that

class MyUser(MyBaseModelMixin, AuthUser):
    ....