Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ramibch opened this issue Nov 4, 2022 · 1 comment

Comments

@ramibch
Copy link

ramibch commented Nov 4, 2022

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!

@tolomea
Copy link
Owner

tolomea commented Nov 4, 2022

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

@tolomea tolomea closed this as completed Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants