Skip to content

Commit

Permalink
Fix #203 -- Prevent access to deferred fields in stdimage file descri…
Browse files Browse the repository at this point in the history
…ptor (#204)
  • Loading branch information
chaosk authored and codingjoe committed Nov 23, 2018
1 parent 1ff71ac commit 920c68b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion stdimage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ def set_variations(self, instance=None, **kwargs):
:param instance: FileField
"""
if getattr(instance, self.name):
deferred_field = self.name in instance.get_deferred_fields()
if not deferred_field and getattr(instance, self.name):
field = getattr(instance, self.name)
if field._committed:
for name, variation in list(self.variations.items()):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ def test_custom_render_variations(self, db):
assert instance.image.thumbnail.width == 100
assert instance.image.thumbnail.height == 100

def test_defer(self, db, django_assert_num_queries):
"""
`set_variations` does not access a deferred field.
Accessing a deferred field would cause Django to do
a second implicit database query.
"""
instance = ResizeModel.objects.create(image=self.fixtures['100.gif'])
with django_assert_num_queries(1):
deferred = ResizeModel.objects.only('pk').get(pk=instance.pk)
with django_assert_num_queries(1):
deferred.image
assert instance.image.thumbnail == deferred.image.thumbnail


class TestUtils(TestStdImage):
"""Tests Utils"""
Expand Down

0 comments on commit 920c68b

Please sign in to comment.