diff --git a/README.md b/README.md index 3c56233..541cb80 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,11 @@ from pictures.models import PictureField class Profile(models.Model): title = models.CharField(max_length=255) - picture = PictureField(upload_to="avatars") + picture = PictureField( + upload_to="avatars", width_field="picture_width", height_field="picture_height" + ) + picture_width = models.PositiveIntegerField(editable=False) + picture_height = models.PositiveIntegerField(editable=False) ``` ```html @@ -157,7 +161,11 @@ class Profile(models.Model): picture = PictureField( upload_to="avatars", aspect_ratios=[None, "1/1", "3/2", "16/9"], + width_field="picture_width", + height_field="picture_height", ) + picture_width = models.PositiveIntegerField(editable=False) + picture_height = models.PositiveIntegerField(editable=False) ``` ```html @@ -235,10 +243,15 @@ class Profile(models.Model): validators=[ MinSizeValidator(400, 300), # At least 400x300 pixels MaxSizeValidator(4096, 4096), # At most 4096x4096 pixels - ] + ], + width_field="picture_width", + height_field="picture_height", ) + picture_width = models.PositiveIntegerField(editable=False) + picture_height = models.PositiveIntegerField(editable=False) + -Use `None` to limit only one dimension: `MaxSizeValidator(2048, None)` limits only width. +# Use `None` to limit only one dimension: `MaxSizeValidator(2048, None)` limits only width. ``` > [!IMPORTANT] diff --git a/pictures/models.py b/pictures/models.py index 4b3f326..77500d6 100644 --- a/pictures/models.py +++ b/pictures/models.py @@ -291,7 +291,7 @@ def _check_aspect_ratios(self): return errors def _check_width_height_field(self): - if None in self.aspect_ratios and not (self.width_field and self.height_field): + if not (self.width_field and self.height_field): return [ checks.Warning( "width_field and height_field attributes are missing", diff --git a/tests/test_models.py b/tests/test_models.py index 00fa40c..b6af2ea 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -825,9 +825,19 @@ def test_check_aspect_ratios(self): assert errors assert errors[0].id == "fields.E100" - def test_check_width_height_field(self): - assert not PictureField(aspect_ratios=["3/2"])._check_width_height_field() - with override_field_aspect_ratios(Profile.picture.field, [None]): + @pytest.mark.parametrize( + "aspect_ratios", + [ + ("3/2",), + (None,), + ( + "3/2", + None, + ), + ], + ) + def test_check_width_height_field(self, aspect_ratios): + with override_field_aspect_ratios(Profile.picture.field, aspect_ratios): errors = Profile.picture.field._check_width_height_field() assert errors assert errors[0].id == "fields.E101"