Considerations when replacing a Mixin with abstract Model subclass?

24 views Asked by At

I have multiple Models that all need some shared functionality.

My existing code uses a Mixin class:

class MyMixing:
    some_class_variable = None

    @classmethod
    def get_headers(cls):
        return [field.name for field in cls._meta.fields]        
    # ...

and my models look like:

class Something(models.Model):
    name = models.CharField(max_length=50)

The above code shows an example, where my IDE shows error, because MyMixin doesn't have _meta.

And there are multiple such issues, especially as I'm trying to add some more common functionality

So I'm thinking it might be better to replace MyMixin with MyModel:

class MyModel(models.Model):
    class Meta:
        abstract = True
    some_class_variable = None

    @classmethod
    def get_headers(cls):
        return [field.name for field in cls._meta.fields]

then my models will look like:

class Something(MyModel):
    name = models.CharField(max_length=50)

Could this mess up my existing data?

Other than database (and, of course, silly typos), is there anything else that might go wrong with that change?

0

There are 0 answers