_custom_image1.rst 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. Step 1: Create a custom image model in a new app
  2. ------------------------------------------------
  3. It is imperative that the custom image model lives in a Django app which does
  4. not rely on or import ``wagtailcrx``. It is recommended to create a separate
  5. "pure" app to contain custom image and document models for your site. Failure
  6. to separate the custom image model will create a circular dependency issue in
  7. migrations.
  8. Create an empty Django app, ours will be named ``mediamodels``:
  9. .. code-block:: console
  10. $ django-admin startapp mediamodels
  11. In ``mediamodels/models.py``, add your custom image model code, following the
  12. `Wagtail custom image sample code <https://docs.wagtail.io/en/stable/advanced_topics/images/custom_image_model.html>`_:
  13. .. code-block:: python
  14. # models.py
  15. from django.db import models
  16. from wagtail.images.models import Image, AbstractImage, AbstractRendition
  17. class CustomImage(AbstractImage):
  18. # Add any extra fields to image here
  19. # eg. To add a caption field:
  20. # caption = models.CharField(max_length=255, blank=True)
  21. admin_form_fields = Image.admin_form_fields + (
  22. # Then add the field names here to make them appear in the form:
  23. # 'caption',
  24. )
  25. class CustomRendition(AbstractRendition):
  26. image = models.ForeignKey(CustomImage, on_delete=models.CASCADE, related_name='renditions')
  27. class Meta:
  28. unique_together = (
  29. ('image', 'filter_spec', 'focal_point_key'),
  30. )