Skip to content

Commit

Permalink
Fix deepcopy & pickle for unsaved model instances & empty image fields (
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar authored Nov 6, 2023
1 parent a1427a5 commit bc9af2a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions stdimage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ def __getstate__(self):
state = super().__getstate__()
state["variations"] = {}
for variation_name in self.field.variations:
variation = getattr(self, variation_name)
variation_state = variation.__getstate__()
state["variations"][variation_name] = variation_state
if variation := getattr(self, variation_name, None):
variation_state = variation.__getstate__()
state["variations"][variation_name] = variation_state
return state

def __setstate__(self, state):
Expand Down Expand Up @@ -207,7 +207,7 @@ def __init__(
render_variations=True,
force_min_size=False,
delete_orphans=False,
**kwargs
**kwargs,
):
"""
Standardized ImageField for Django.
Expand Down
14 changes: 14 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ def test_defer(self, db, django_assert_num_queries):
deferred.image
assert instance.image.thumbnail == deferred.image.thumbnail

@pytest.mark.django_db
def test_variations_deepcopy_unsaved(self):
instance_original = ResizeModel(image=self.fixtures["600x400.jpg"])
instance = deepcopy(instance_original)
assert isinstance(instance.image, StdImageFieldFile)
assert instance.image == instance_original.image

@pytest.mark.django_db
def test_variations_deepcopy_without_image(self):
instance_original = ThumbnailModel.objects.create(image=None)
instance = deepcopy(instance_original)
assert isinstance(instance.image, StdImageFieldFile)
assert instance.image == instance_original.image

@pytest.mark.django_db
def test_variations_deepcopy(self):
"""Tests test_variations() with a deep copied object"""
Expand Down

0 comments on commit bc9af2a

Please sign in to comment.