Commit aa77c47b authored by Jamesie Pic's avatar Jamesie Pic
Browse files

Performance

Representative.addresses.phones is reverse foreign key on a reverse
foreign key so that's impossible to use prefetch_related with.

It would need to be denormalized before being usable, let's wait until
someone requires this specific feature.
parent 511ba165
from django.db import models
from rest_framework import (
filters,
viewsets,
......@@ -12,9 +14,11 @@ from representatives.serializers import (
)
from .models import (
Address,
Constituency,
Group,
Mandate,
Phone,
Representative,
)
......@@ -44,6 +48,23 @@ class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet):
search_fields = ('first_name', 'last_name', 'slug')
ordering_fields = ('id', 'birth_date', 'last_name', 'full_name')
def get_queryset(self):
qs = super(RepresentativeViewSet, self).get_queryset()
qs = qs.prefetch_related(
'email_set',
'website_set',
models.Prefetch(
'address_set',
queryset=Address.objects.select_related('country')
),
models.Prefetch(
'phone_set',
queryset=Phone.objects.select_related('address__country')
),
'mandates',
)
return qs
def list(self, request):
self.serializer_class = RepresentativeSerializer
return super(RepresentativeViewSet, self).list(request)
......@@ -71,7 +92,11 @@ class MandateViewSet(viewsets.ReadOnlyModelViewSet):
'group__abbreviation': ['exact'],
}
search_fields = ('group__name', 'group__abbreviation')
# ordering_fields = ()
def get_queryset(self):
qs = super(MandateViewSet, self).get_queryset()
qs = qs.select_related('representative')
return qs
class ConstituencyViewSet(viewsets.ReadOnlyModelViewSet):
......
......@@ -62,13 +62,12 @@ class PhoneSerializer(serializers.ModelSerializer):
class AddressSerializer(serializers.ModelSerializer):
country = CountrySerializer()
phones = PhoneSerializer(many=True)
class Meta:
model = models.Address
fields = ('country', 'city', 'street',
'number', 'postcode', 'floor',
'office_number', 'kind', 'phones',
'office_number', 'kind',
)
......
Supports Markdown
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