Skip to content

Commit 0c5b5e5

Browse files
committed
Add deprecation warning and migration notes.
1 parent 0730fcc commit 0c5b5e5

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

README.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,92 @@
44

55
# Django Standardized Image Field
66

7+
This package has been deprecated in favor of [django-pictures][django-pictures].
8+
9+
## Migration Instructions
10+
11+
First, make sure you understand the differences between the two packages and
12+
how to serve images in a modern web application via the [picture][picture-tag]-Element.
13+
14+
Next, follow the setup instructions for [django-pictures][django-pictures].
15+
16+
Once you are set up, change your models to use the new `PictureField` and provide the
17+
`aspect_ratios` you'd like to serve. Do create migrations just yet.
18+
19+
This step should be followed by changing your templates and frontend.
20+
The new placeholders feature for local development should help you
21+
to do this almost effortlessly.
22+
23+
Finally, run `makemigrations` and replace the `AlterField` operation with
24+
`AlterPictureField`.
25+
26+
We highly recommend to use Django's `image_width` and `image_height` fields, to avoid
27+
unnecessary IO. If you can add these fields to your model, you can use the following
28+
snippet to populate them:
29+
30+
```python
31+
import django.core.files.storage
32+
from django.db import migrations, models
33+
import pictures.models
34+
from pictures.migrations import AlterPictureField
35+
36+
def forward(apps, schema_editor):
37+
for obj in apps.get_model("my-app.MyModel").objects.all().iterator():
38+
obj.image_width = obj.logo.width
39+
obj.image_height = obj.logo.height
40+
obj.save(update_fields=["image_height", "image_width"])
41+
42+
def backward(apps, schema_editor):
43+
apps.get_model("my-app.MyModel").objects.all().update(
44+
image_width=None,
45+
image_height=None,
46+
)
47+
48+
class Migration(migrations.Migration):
49+
dependencies = [
50+
('my-app', '0001_initial'),
51+
]
52+
53+
operations = [
54+
migrations.AddField(
55+
model_name="mymodel",
56+
name="image_height",
57+
field=models.PositiveIntegerField(editable=False, null=True),
58+
),
59+
migrations.AddField(
60+
model_name="mymodel",
61+
name="image_width",
62+
field=models.PositiveIntegerField(editable=False, null=True),
63+
),
64+
migrations.RunPython(forward, backward),
65+
AlterPictureField(
66+
model_name="mymodel",
67+
name="image",
68+
field=pictures.models.PictureField(
69+
aspect_ratios=["3/2", "3/1"],
70+
breakpoints={"desktop": 1024, "mobile": 576},
71+
container_width=1200,
72+
file_types=["WEBP"],
73+
grid_columns=12,
74+
height_field="image_height",
75+
pixel_densities=[1, 2],
76+
storage=django.core.files.storage.FileSystemStorage(),
77+
upload_to="pictures/",
78+
verbose_name="image",
79+
width_field="image_width",
80+
),
81+
),
82+
]
83+
```
84+
85+
[django-pictures]: https://github.com/codingjoe/django-pictures
86+
[picture-tag]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture
87+
788
## Why would I want this?
889

990
This is a drop-in replacement for the [Django ImageField](https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ImageField) that provides a standardized way to handle image uploads.
1091
It is designed to be as easy to use as possible, and to provide a consistent interface for all image fields.
11-
It allows images to be presented in various size variants (eg:thumbnails, mid, and hi-res versions)
92+
It allows images to be presented in various size variants (eg:thumbnails, mid, and hi-res versions)
1293
and it provides a way to handle images that are too large with validators.
1394

1495

@@ -103,7 +184,7 @@ You can use a function for the `upload_to` argument. Using [Django Dynamic Filen
103184

104185
This allows images to be given unique paths and filenames based on the model instance.
105186

106-
Example
187+
Example
107188

108189
```python
109190
from django.db import models

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ norecursedirs=venv env .eggs
6363
DJANGO_SETTINGS_MODULE=tests.settings
6464
addopts = --cov=stdimage --nomigrations --tb=short
6565
filterwarnings =
66-
error
66+
ignore::DeprecationWarning
6767

6868
[coverage:run]
6969
source = .

stdimage/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import os
3+
import warnings
34
from io import BytesIO
45

56
from django.core.files.base import ContentFile
@@ -18,6 +19,14 @@
1819
logger = logging.getLogger()
1920

2021

22+
warnings.warn(
23+
"The django-stdimage is deprecated in favor of django-pictures.\n"
24+
"Migration instructions are available in the README:\n"
25+
"https://github.com/codingjoe/django-stdimage#migration-instructions",
26+
DeprecationWarning,
27+
)
28+
29+
2130
class StdImageFileDescriptor(ImageFileDescriptor):
2231
"""The variation property of the field is accessible in instance cases."""
2332

0 commit comments

Comments
 (0)