'Proper way to do Mixins for Django models

I have the following mixins:

class AbandonableMixin(object):
    is_abandoned = models.BooleanField(
        default=False, verbose_name=_('Abandoned?'))


class ReadyMixin(object):
    is_ready = models.BooleanField(
        default=False, verbose_name=_('Ready?'))


class StoppableMixin(object):
    is_stopped = models.BooleanField(
        default=False, verbose_name=_('Stopped?'))

I would like to use these in my class like ordinary mixins:

class MyObject(models.Model, AbandonableMixin, StoppableMixin): 
... 

class MySecondObject(models.Model, ReadyMixin, StoppableMixin):
...

This results in the following error:

TypeError: Cannot create a consistent method resolution
order (MRO) for bases Model, AbandonableMixin

What am I doing wrong?



Solution 1:[1]

@willem-van-onsem answered, but I will try to give more detailed answer (also based on this article), you need to inherit from Django base model and use abstract models:

class AbandonableMixin(models.Model):
    is_abandoned = models.BooleanField()

    class Meta:
        abstract = True


class StoppableMixin(models.Model):
    is_stopped = models.BooleanField()

    class Meta:
        abstract = True


class MyObject(AbandonableMixin, StoppableMixin): 
    ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexey Shrub