Skip to main content

Tools for automatically prefetching related objects in Django and Django-rest-framework

Project description

Django auto-prefetching

NOTICE: THIS PROJECT IS A WORK IN PROGRESS, AND IS NOT PUBLISHED TO PYPI YET

Automatic prefetching of related objects for Django Rest Framework.

Inside your ViewSets get_queryset add the following code:

    def get_queryset():
        qs = YOUR_MODEL.objects.all() # Or whatever queryset you want to use
        qs = prefetch(self.get_serializer_class(), queryset) # This line prefetches the related model depending on the serializer.
        return qs

Unresolved issues

  • If you forget to add many=True to a serializer that has the reverse side of the ForeignKey we calculate the wrong prefetch_related fields and we get an error. We can't catch this error early because it's only thrown when the queryset is evaluated
  • We can't prefetch anything that's accessed in serializer method fields.

LICENSE:

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-auto-prefetching, version 0.1.3
Filename, size File type Python version Upload date Hashes
Filename, size django_auto_prefetching-0.1.3-py2.py3-none-any.whl (18.5 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size django_auto_prefetching-0.1.3.tar.gz (10.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page