2
0

custom_document_model.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. .. _custom_document_model:
  2. =====================
  3. Custom document model
  4. =====================
  5. An alternate ``Document`` model can be used to add custom behaviour and
  6. additional fields.
  7. You need to complete the following steps in your project to do this:
  8. - Create a new document model that inherits from
  9. ``wagtail.documents.models.AbstractDocument``. This is where you would
  10. add additional fields.
  11. - Point ``WAGTAILDOCS_DOCUMENT_MODEL`` to the new model.
  12. Here's an example:
  13. .. code-block:: python
  14. # models.py
  15. from wagtail.documents.models import Document, AbstractDocument
  16. class CustomDocument(AbstractDocument):
  17. # Custom field example:
  18. source = models.CharField(
  19. max_length=255,
  20. blank=True,
  21. null=True
  22. )
  23. admin_form_fields = Document.admin_form_fields + (
  24. # Add all custom fields names to make them appear in the form:
  25. 'source',
  26. )
  27. .. versionchanged:: 2.12
  28. Fields on a custom document model can now be defined as required (``blank=False``).
  29. Then in your settings module:
  30. .. code-block:: python
  31. # Ensure that you replace app_label with the app you placed your custom
  32. # model in.
  33. WAGTAILDOCS_DOCUMENT_MODEL = 'app_label.CustomDocument'
  34. .. topic:: Migrating from the builtin document model
  35. When changing an existing site to use a custom document model, no documents
  36. will be copied to the new model automatically. Copying old documents to the
  37. new model would need to be done manually with a
  38. :ref:`data migration <django:data-migrations>`.
  39. Any templates that reference the builtin document model will still continue
  40. to work as before.
  41. Referring to the document model
  42. ===============================
  43. .. module:: wagtail.documents
  44. .. autofunction:: get_document_model
  45. .. autofunction:: get_document_model_string