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 option to enable globally #131

Open
xi opened this issue Jan 5, 2022 · 3 comments
Open

Add option to enable globally #131

xi opened this issue Jan 5, 2022 · 3 comments

Comments

@xi
Copy link

xi commented Jan 5, 2022

Description

As far as I understand, this package aims at preventing common mistakes, especially for developers who are less experienced with the ORM. As such I believe that it is important to have as low a threshold as possible. Ideally it would be "just install this package and everything will be faster".

I imaging this could be achieved by patching the django classes rather than extending them. Of course that would also be a lot less clean. So I was wondering whether it would be possible to have this as an opt-in setting. Something like USE_GLOBAL_AUTO_PREFETCH = True.

What do you think?

@tolomea
Copy link
Owner

tolomea commented Jan 5, 2022

It's an interesting idea.

In the 7 or 8 years since we initially wrote this I've never imposed it on third party packages, but I don't see any concern doing that and there is already an open ticket for merging it into Django proper.

I'm not sure how hard it will be to support across different versions of Django or how much code could be shared with the current version.

@xi
Copy link
Author

xi commented Jan 5, 2022

I believe it would be sufficient to have a model.py with the following content:

from django.db import models
import auto_prefetch

models.ForeignKey.forward_related_accessor_class = auto_prefetch.ForwardManyToOneDescriptor
models.OneToOneField.forward_related_accessor_class = auto_prefetch.ForwardOneToOneDescriptor
models.OneToOneField.related_accessor_class = auto_prefetch.ReverseOneToOneDescriptor
models.QuerySet = auto_prefetch.QuerySet

Then adding auth_prefetch to INSTALLED_APPS as early as possible should do the trick, right? I don't think this would add much additional complexity.

@tolomea
Copy link
Owner

tolomea commented Jan 23, 2022

This is an interesting idea that I'm curious to try out when I get some time, you could try it yourself, just create a new app that does what you suggest and put it at the top of your INSTALLED_APPS

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