custom_document_model.rst 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. Then in your settings module:
  28. .. code-block:: python
  29. # Ensure that you replace app_label with the app you placed your custom
  30. # model in.
  31. WAGTAILDOCS_DOCUMENT_MODEL = 'app_label.CustomDocument'
  32. .. topic:: Migrating from the builtin document model
  33. When changing an existing site to use a custom document model, no documents
  34. will be copied to the new model automatically. Copying old documents to the
  35. new model would need to be done manually with a
  36. :ref:`data migration <django:data-migrations>`.
  37. Any templates that reference the builtin document model will still continue
  38. to work as before.
  39. Referring to the document model
  40. ===============================
  41. .. module:: wagtail.documents
  42. .. autofunction:: get_document_model
  43. .. autofunction:: get_document_model_string