0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00
wagtail/docs/advanced_topics/images/custom_image_model.rst

73 lines
2.1 KiB
ReStructuredText
Raw Normal View History

.. _custom_image_model:
===================
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:
Fix documentation indentation Fix code block indentation in tutorial.rst Prevent it from being displayed as a quote. Fix indentation in pages.rst Fix indentation in indexing.rst Fix indentation in searching.rst Fix indentation in backends.rst Fix indentation in renditions.rst Fix indentation in custom_image_model.rst Fix indentation in feature_detection.rst Fix indentation in image_serve_view.rst Fix indentation in custom_document_model.rst Fix indentation in i18n.rst Fix indentation in privacy.rst Fix indentation in page_editing_interface.rst Fix indentation in rich_text_internals.rst Fix indentation in extending_hallo.rst Fix indentation in configuration.rst Fix indentation in usage.rst Fix indentation in theory.rst Fix indentation in model_reference.rst Fix indentation in queryset_reference.rst Configure editors to indent .rst files with 2 spaces In order for the documentation to be styled correctly, the generator depends on indentation. Too much indentation can result in the content being wrapped in a quote block, which looks bad. Fix indentation in sitemaps.rst Fix indentation in frontendcache.rst Fix indentation in routablepage.rst Fix indentation in table_block.rst Fix routablepage.rst autodocs disppearing Fix indentation in table_block.rst Fix indentation in redirects.rst Fix indentation in table_documentation-modes.rst Fix indentation in browser_issues.rst Fix indentation in release_process.rst Fix indentation of release notes One more indent fix in the release notes Fix indentation warnings Fix warning about undefined label in docs Error during `make html`: wagtail/docs/releases/1.7.rst:25: WARNING: undefined label: jpeg_image_quality
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.
Here's an example:
.. code-block:: python
# models.py
from django.db import models
from wagtail.images.models import Image, AbstractImage, AbstractRendition
class CustomImage(AbstractImage):
# Add any extra fields to image here
# eg. To add a caption field:
# caption = models.CharField(max_length=255, blank=True)
admin_form_fields = Image.admin_form_fields + (
# Then add the field names here to make them appear in the form:
# 'caption',
)
class CustomRendition(AbstractRendition):
image = models.ForeignKey(CustomImage, on_delete=models.CASCADE, related_name='renditions')
class Meta:
unique_together = (
('image', 'filter_spec', 'focal_point_key'),
)
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
When changing an existing site to use a custom image model, no images will
be copied to the new model automatically. Copying old images to the new
model would need to be done manually with a
:ref:`data migration <django:data-migrations>`.
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 15:54:34 +02:00
.. _custom_image_model_referring_to_image_model:
Referring to the image model
============================
.. module:: wagtail.images
.. autofunction:: get_image_model
.. autofunction:: get_image_model_string