Skip to content

Commit 72e263e

Browse files
committed
Have get_crop play nicer with prefetch_related.
1 parent 6707f54 commit 72e263e

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

cropduster/templatetags/cropduster_tags.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import six
44

55
from django import template
6-
from cropduster.models import Image, Thumb
6+
from cropduster.models import Image
77
from cropduster.resizing import Size
88

99

@@ -33,23 +33,24 @@ def get_crop(image, crop_name, **kwargs):
3333
<img src="{{ img.url }}">
3434
"""
3535

36-
if not image or not image.related_object:
36+
if not image:
3737
return None
3838

3939
if len(kwargs) > 0:
4040
warnings.warn("All get_crop kwargs have been deprecated", DeprecationWarning)
4141

4242
data = {}
43+
thumbs = {thumb.name: thumb for thumb in image.related_object.thumbs.all()}
4344
try:
44-
thumb = image.related_object.thumbs.get(name=crop_name)
45-
except Thumb.DoesNotExist:
45+
thumb = thumbs[crop_name]
46+
except KeyError:
4647
if crop_name == "original":
4748
thumb = image.related_object
4849
else:
4950
return None
5051

5152
data.update({
52-
"url": thumb.url,
53+
"url": Image.get_file_for_size(image=image, size_name=crop_name),
5354
"width": thumb.width,
5455
"height": thumb.height,
5556
"attribution": image.related_object.attribution,

0 commit comments

Comments
 (0)