|
4 | 4 |
|
5 | 5 | # Django Standardized Image Field
|
6 | 6 |
|
| 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 | + |
7 | 88 | ## Why would I want this?
|
8 | 89 |
|
9 | 90 | 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.
|
10 | 91 | 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) |
12 | 93 | and it provides a way to handle images that are too large with validators.
|
13 | 94 |
|
14 | 95 |
|
@@ -103,7 +184,7 @@ You can use a function for the `upload_to` argument. Using [Django Dynamic Filen
|
103 | 184 |
|
104 | 185 | This allows images to be given unique paths and filenames based on the model instance.
|
105 | 186 |
|
106 |
| -Example |
| 187 | +Example |
107 | 188 |
|
108 | 189 | ```python
|
109 | 190 | from django.db import models
|
|
0 commit comments