custom_image_model.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. .. _custom_image_model:
  2. ===================
  3. Custom image models
  4. ===================
  5. The ``Image`` model can be customised, allowing additional fields to be added
  6. to images.
  7. To do this, you need to add two models to your project:
  8. - The image model itself that inherits from
  9. ``wagtail.wagtailimages.models.AbstractImage``. This is where you would add
  10. your additional fields
  11. - The renditions model that inherits from
  12. ``wagtail.wagtailimages.models.AbstractRendition``. This is used to store
  13. renditions for the new model.
  14. Here's an example:
  15. .. code-block:: python
  16. # models.py
  17. from django.db import models
  18. from wagtail.wagtailimages.models import Image, AbstractImage, AbstractRendition
  19. class CustomImage(AbstractImage):
  20. # Add any extra fields to image here
  21. # eg. To add a caption field:
  22. # caption = models.CharField(max_length=255, blank=True)
  23. admin_form_fields = Image.admin_form_fields + (
  24. # Then add the field names here to make them appear in the form:
  25. # 'caption',
  26. )
  27. class CustomRendition(AbstractRendition):
  28. image = models.ForeignKey(CustomImage, related_name='renditions')
  29. class Meta:
  30. unique_together = (
  31. ('image', 'filter_spec', 'focal_point_key'),
  32. )
  33. .. versionchanged:: 1.10
  34. In previous versions of Wagtail it was necessary to connect signal handlers to handle deletion of image files. As of Wagtail 1.10 this is now handled automatically.
  35. .. note::
  36. Fields defined on a custom image model must either be set as non-required
  37. (``blank=True``), or specify a default value - this is because uploading
  38. the image and entering custom data happen as two separate actions, and
  39. Wagtail needs to be able to create an image record immediately on upload.
  40. Then set the ``WAGTAILIMAGES_IMAGE_MODEL`` setting to point to it:
  41. .. code-block:: python
  42. WAGTAILIMAGES_IMAGE_MODEL = 'images.CustomImage'
  43. .. topic:: Migrating from the builtin image model
  44. When changing an existing site to use a custom image model, no images will
  45. be copied to the new model automatically. Copying old images to the new
  46. model would need to be done manually with a
  47. `data migration <https://docs.djangoproject.com/en/1.8/topics/migrations/#data-migrations>`_.
  48. Any templates that reference the builtin image model will still continue to
  49. work as before but would need to be updated in order to see any new images.
  50. .. _custom_image_model_referring_to_image_model:
  51. Referring to the image model
  52. ============================
  53. .. module:: wagtail.wagtailimages
  54. .. autofunction:: get_image_model
  55. .. autofunction:: get_image_model_string