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.

Source Distribution

django_auto_prefetching-0.1.1.tar.gz (10.6 kB view hashes)

Uploaded Source

Built Distribution

django_auto_prefetching-0.1.1-py2.py3-none-any.whl (18.7 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page