Skip to content

Commit

Permalink
optimize GET assets/ to reduce queries and improve speed dramatically…
Browse files Browse the repository at this point in the history
… on large data sets
  • Loading branch information
hburgund committed Apr 20, 2018
1 parent 0b4815a commit 1ecb0f2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
6 changes: 3 additions & 3 deletions roundware/api2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Meta:
'tags',
'session',
'project',
'envelope_set'
'envelope'
)
localized_fields = ['loc_description', 'loc_alt_text']

Expand All @@ -86,8 +86,8 @@ def to_representation(self, obj):
result["language_id"] = result["language"]
del result["language"]

result["envelope_ids"] = result["envelope_set"]
del result["envelope_set"]
result["envelope_ids"] = result["envelope"]
del result["envelope"]

result["description_loc_ids"] = result["loc_description"]
del result["loc_description"]
Expand Down
8 changes: 7 additions & 1 deletion roundware/api2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
from datetime import datetime
from distutils.util import strtobool
from collections import OrderedDict
try:
from profiling import profile
except ImportError: # pragma: no cover
pass

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -90,14 +94,16 @@ class AssetViewSet(viewsets.GenericViewSet, AssetPaginationMixin,):
"""

# TODO: Implement DjangoObjectPermissions
queryset = Asset.objects.all()
queryset = Asset.objects.prefetch_related('tags', 'loc_description', 'loc_alt_text', 'envelope') \
.select_related('session', 'project', 'language', 'initialenvelope').all()
permission_classes = (IsAuthenticated,)
pagination_class = AssetPagination
serializer_class = serializers.AssetSerializer
filter_backends = (DjangoFilterBackend, OrderingFilter,)
ordering_fields = ('id', 'session_id', 'audiolength', 'weight', 'volume')
filter_class = AssetFilterSet

# @profile(stats=True)
def list(self, request):
"""
GET api/2/assets/ - retrieve list of Assets filtered by parameters
Expand Down
20 changes: 20 additions & 0 deletions roundware/rw/migrations/0029_envelope_related_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2018-04-18 17:10
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('rw', '0028_add_uielement_models'),
]

operations = [
migrations.AlterField(
model_name='envelope',
name='assets',
field=models.ManyToManyField(blank=True, related_name='envelope', to='rw.Asset'),
),
]
2 changes: 1 addition & 1 deletion roundware/rw/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class Envelope(models.Model):
"""
session = models.ForeignKey('Session', blank=True)
created = models.DateTimeField(default=datetime.now)
assets = models.ManyToManyField(Asset, blank=True)
assets = models.ManyToManyField(Asset, blank=True, related_name='envelope')

def __unicode__(self):
return str(self.id) + ": Session id: " + str(self.session.id)
Expand Down

0 comments on commit 1ecb0f2

Please sign in to comment.