custom_document_model.rst 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. # This must be set to allow Wagtail to create a document instance
  21. # on upload.
  22. blank=True,
  23. null=True
  24. )
  25. admin_form_fields = Document.admin_form_fields + (
  26. # Add all custom fields names to make them appear in the form:
  27. 'source',
  28. )
  29. .. note::
  30. Fields defined on a custom document model must either be set as non-required
  31. (``blank=True``), or specify a default value. This is because uploading the
  32. document and entering custom data happens as two separate actions. Wagtail
  33. needs to be able to create a document record immediately on upload.
  34. Then in your settings module:
  35. .. code-block:: python
  36. # Ensure that you replace app_label with the app you placed your custom
  37. # model in.
  38. WAGTAILDOCS_DOCUMENT_MODEL = 'app_label.CustomDocument'
  39. .. topic:: Migrating from the builtin document model
  40. When changing an existing site to use a custom document model, no documents
  41. will be copied to the new model automatically. Copying old documents to the
  42. new model would need to be done manually with a
  43. :ref:`data migration <django:data-migrations>`.
  44. Any templates that reference the builtin document model will still continue
  45. to work as before.
  46. Referring to the document model
  47. ===============================
  48. .. module:: wagtail.documents
  49. .. autofunction:: get_document_model
  50. .. autofunction:: get_document_model_string