Fixed #34517 -- Avoided connection post_init signal to ImageField without width/height fields.

This commit is contained in:
Orhan Hirsch 2023-04-26 09:22:43 +02:00 committed by Mariusz Felisiak
parent 3b62d8c83e
commit ea53e7c09f
3 changed files with 15 additions and 5 deletions

View File

@ -441,7 +441,8 @@ class ImageField(FileField):
# after their corresponding image field don't stay cleared by # after their corresponding image field don't stay cleared by
# Model.__init__, see bug #11196. # Model.__init__, see bug #11196.
# Only run post-initialization dimension update on non-abstract models # Only run post-initialization dimension update on non-abstract models
if not cls._meta.abstract: # with width_field/height_field.
if not cls._meta.abstract and (self.width_field or self.height_field):
signals.post_init.connect(self.update_dimension_fields, sender=cls) signals.post_init.connect(self.update_dimension_fields, sender=cls)
def update_dimension_fields(self, instance, force=False, *args, **kwargs): def update_dimension_fields(self, instance, force=False, *args, **kwargs):
@ -457,10 +458,8 @@ class ImageField(FileField):
Dimensions can be forced to update with force=True, which is how Dimensions can be forced to update with force=True, which is how
ImageFileDescriptor.__set__ calls this method. ImageFileDescriptor.__set__ calls this method.
""" """
# Nothing to update if the field doesn't have dimension fields or if # Nothing to update if the field is deferred.
# the field is deferred. if self.attname not in instance.__dict__:
has_dimension_fields = self.width_field or self.height_field
if not has_dimension_fields or self.attname not in instance.__dict__:
return return
# getattr will call the ImageFileDescriptor's __get__ method, which # getattr will call the ImageFileDescriptor's __get__ method, which

View File

@ -379,6 +379,9 @@ Miscellaneous
* The undocumented ``Query.annotation_select_mask`` attribute is changed from a * The undocumented ``Query.annotation_select_mask`` attribute is changed from a
set of strings to an ordered list of strings. set of strings to an ordered list of strings.
* ``ImageField.update_dimension_fields()`` is no longer called on the
``post_init`` signal if ``width_field`` and ``height_field`` are not set.
.. _deprecated-features-5.0: .. _deprecated-features-5.0:
Features deprecated in 5.0 Features deprecated in 5.0

View File

@ -5,6 +5,7 @@ from unittest import skipIf
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.files import File from django.core.files import File
from django.core.files.images import ImageFile from django.core.files.images import ImageFile
from django.db.models import signals
from django.test import TestCase from django.test import TestCase
from django.test.testcases import SerializeMixin from django.test.testcases import SerializeMixin
@ -328,6 +329,13 @@ class ImageFieldNoDimensionsTests(ImageFieldTwoDimensionsTests):
PersonModel = Person PersonModel = Person
def test_post_init_not_connected(self):
person_model_id = id(self.PersonModel)
self.assertNotIn(
person_model_id,
[sender_id for (_, sender_id), *_ in signals.post_init.receivers],
)
@skipIf(Image is None, "Pillow is required to test ImageField") @skipIf(Image is None, "Pillow is required to test ImageField")
class ImageFieldOneDimensionTests(ImageFieldTwoDimensionsTests): class ImageFieldOneDimensionTests(ImageFieldTwoDimensionsTests):