mirror of
https://github.com/pinry/pinry.git
synced 2025-11-14 00:55:43 +01:00
Loanding pins does no N+1 queries anymore
This saves another three queries per pin, bringing to total query count down to 8. See issue #85 .
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from tastypie import fields
|
from tastypie import fields
|
||||||
from tastypie.authorization import DjangoAuthorization
|
from tastypie.authorization import DjangoAuthorization
|
||||||
from tastypie.constants import ALL, ALL_WITH_RELATIONS
|
from tastypie.constants import ALL, ALL_WITH_RELATIONS
|
||||||
@@ -59,7 +60,10 @@ class UserResource(ModelResource):
|
|||||||
|
|
||||||
def filter_generator_for(size):
|
def filter_generator_for(size):
|
||||||
def wrapped_func(bundle, **kwargs):
|
def wrapped_func(bundle, **kwargs):
|
||||||
return bundle.obj.get_by_size(size)
|
for thumbnail in bundle.obj._prefetched_objects_cache['thumbnail']:
|
||||||
|
if thumbnail.size == size:
|
||||||
|
return thumbnail
|
||||||
|
raise DoesNotExist()
|
||||||
return wrapped_func
|
return wrapped_func
|
||||||
|
|
||||||
|
|
||||||
@@ -136,7 +140,7 @@ class PinResource(ModelResource):
|
|||||||
'submitter': ALL_WITH_RELATIONS
|
'submitter': ALL_WITH_RELATIONS
|
||||||
}
|
}
|
||||||
queryset = Pin.objects.all().select_related('submitter'). \
|
queryset = Pin.objects.all().select_related('submitter'). \
|
||||||
prefetch_related('image', 'tags')
|
prefetch_related('image__thumbnail_set', 'tags')
|
||||||
resource_name = 'pin'
|
resource_name = 'pin'
|
||||||
include_resource_uri = False
|
include_resource_uri = False
|
||||||
always_return_data = True
|
always_return_data = True
|
||||||
|
|||||||
Reference in New Issue
Block a user