Commit ff276c93 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

snf-django: Optimize DB queries in ListCommand

Add options to use 'prefetch_related' and 'select_related' options of
Django queries to optimize the DB queries.
parent 7b2edcc3
......@@ -233,8 +233,15 @@ class ListCommand(BaseCommand):
# Special handling of arguments
self.handle_args(self, *args, **options)
select_related = getattr(self, "select_related", [])
prefetch_related = getattr(self, "prefetch_related", [])
objects = self.object_class.objects
for sr in select_related:
objects = objects.select_related(sr)
for pr in prefetch_related:
objects = objects.prefetch_related(pr)
objects = objects.filter(**self.filters)
objects = objects.exclude(**self.excludes)
except FieldError as e:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment