Skip to content

Not working for multiple levels of inheritance #8

@kekomal

Description

@kekomal

When the mixin is added to a parent abstract model, it does not add the fields to the child.

In my case, this approach to the receiver works better:

def mixin_step(target, clazz):
    for base in clazz.__bases__:
        if issubclass(base, ModelMixin):
            if ModelMixin in base.__bases__:
                base.model_mixin(target)
            mixin_step(target, base)

@receiver(class_prepared)
def mixin(sender, **kwargs):
    mixin_step(sender, sender)

I'm afraid this approach can give problems for non-abstract parent models (it was not my case, so I haven't tested it), so maybe the next approach is better:

def mixin_step(target, clazz):
    for base in clazz.__bases__:
        if issubclass(base, ModelMixin):
            if ModelMixin in base.__bases__:
                base.model_mixin(target)
            try:
                if not (issubclass(base, models.Model) and not base._meta.abstract):
                    mixin_step(target, base)
            except: pass

@receiver(class_prepared)
def mixin(sender, **kwargs):
    mixin_step(sender, sender)

I'm sure this solution can be polished. I only wanted to comment the issue and prompt to a feasible solution.

Regards,
Eneko

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions