2016-01-16 14:19:05 +01:00
|
|
|
.. _custom_image_model:
|
|
|
|
|
2016-10-13 11:55:21 +02:00
|
|
|
===================
|
2016-01-16 14:19:05 +01:00
|
|
|
Custom image models
|
|
|
|
===================
|
|
|
|
|
|
|
|
The ``Image`` model can be customised, allowing additional fields to be added
|
|
|
|
to images.
|
|
|
|
|
|
|
|
To do this, you need to add two models to your project:
|
|
|
|
|
2021-02-05 12:02:05 +01:00
|
|
|
- The image model itself that inherits from ``wagtail.images.models.AbstractImage``. This is where you would add your additional fields
|
|
|
|
- The renditions model that inherits from ``wagtail.images.models.AbstractRendition``. This is used to store renditions for the new model.
|
2016-01-16 14:19:05 +01:00
|
|
|
|
|
|
|
Here's an example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# models.py
|
|
|
|
from django.db import models
|
2016-10-13 11:55:21 +02:00
|
|
|
|
2017-11-17 12:17:58 +01:00
|
|
|
from wagtail.images.models import Image, AbstractImage, AbstractRendition
|
2016-01-16 14:19:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CustomImage(AbstractImage):
|
|
|
|
# Add any extra fields to image here
|
|
|
|
|
|
|
|
# eg. To add a caption field:
|
2016-01-12 20:35:00 +01:00
|
|
|
# caption = models.CharField(max_length=255, blank=True)
|
2016-01-16 14:19:05 +01:00
|
|
|
|
|
|
|
admin_form_fields = Image.admin_form_fields + (
|
|
|
|
# Then add the field names here to make them appear in the form:
|
|
|
|
# 'caption',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CustomRendition(AbstractRendition):
|
2017-12-15 06:56:30 +01:00
|
|
|
image = models.ForeignKey(CustomImage, on_delete=models.CASCADE, related_name='renditions')
|
2016-01-16 14:19:05 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (
|
2016-11-14 19:28:28 +01:00
|
|
|
('image', 'filter_spec', 'focal_point_key'),
|
2016-01-16 14:19:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
Then set the ``WAGTAILIMAGES_IMAGE_MODEL`` setting to point to it:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
WAGTAILIMAGES_IMAGE_MODEL = 'images.CustomImage'
|
|
|
|
|
|
|
|
|
|
|
|
.. topic:: Migrating from the builtin image model
|
|
|
|
|
2017-02-16 18:11:37 +01:00
|
|
|
When changing an existing site to use a custom image model, no images will
|
2016-01-16 14:19:05 +01:00
|
|
|
be copied to the new model automatically. Copying old images to the new
|
|
|
|
model would need to be done manually with a
|
2018-11-14 12:48:32 +01:00
|
|
|
:ref:`data migration <django:data-migrations>`.
|
2016-01-16 14:19:05 +01:00
|
|
|
|
|
|
|
Any templates that reference the builtin image model will still continue to
|
|
|
|
work as before but would need to be updated in order to see any new images.
|
2016-10-13 11:55:21 +02:00
|
|
|
|
2016-10-13 15:54:34 +02:00
|
|
|
.. _custom_image_model_referring_to_image_model:
|
|
|
|
|
2016-10-13 11:55:21 +02:00
|
|
|
Referring to the image model
|
|
|
|
============================
|
|
|
|
|
2017-11-17 12:17:58 +01:00
|
|
|
.. module:: wagtail.images
|
2016-10-13 11:55:21 +02:00
|
|
|
|
|
|
|
.. autofunction:: get_image_model
|
|
|
|
|
|
|
|
.. autofunction:: get_image_model_string
|