tolomea/django-auto-prefetch

Add compatiblity for abstract models or describe in README a trick to use the auto-prefetch with them

ramibch opened this issue · 1 comments

Description

I am not sure how this could be achieved but I would like to use the django-auto-prefetch when having an abstract model.

I have an example like this:

class AbstractModel(auto_prefetch.Model):
    text = models.TextField(max_length=100)
    # more abstract fields...
    created_by = auto_prefetch.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )

    class Meta:
        abstract = True

class ModelA(AbstractModel):
    field_a = models.TextField(max_length=100)
        # more fields for ModelA...

class ModelB(AbstractModel):
    field_b = models.TextField(max_length=100)
        # more fields for ModelB...

When I run python manage.py makemigrations I get the following ERRORS;

myapp.ModelA: (auto_prefetch.E001) ModelA inherits from auto_prefetch.Model, but its Meta class does not inherit from auto_prefetch.Model.Meta
myapp.ModelB: (auto_prefetch.E001) ModelB inherits from auto_prefetch.Model, but its Meta class does not inherit from auto_prefetch.Model.Meta

Is there any trick to use auto-prefetch on an abstract model or do I have to stop using the abstract model? Maybe saving some microseconds in the queries is better than saving some lines of code 😅

Many thanks!

The line class Meta: needs to be changed to class Meta(auto_prefetch.Model.Meta):