README.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. django mongodbforms
  2. ===================
  3. This is an implementation of django's model forms for mongoengine
  4. documents.
  5. Requirements
  6. ------------
  7. - Django >= 1.4
  8. - `mongoengine <http://mongoengine.org/>`__ >= 0.8.3
  9. Supported field types
  10. ---------------------
  11. Mongodbforms supports all the fields that have a simple representation
  12. in Django's formfields (IntField, TextField, etc). In addition it also
  13. supports ``ListFields`` and ``MapFields``.
  14. File fields
  15. ~~~~~~~~~~~
  16. Mongodbforms handles file uploads just like the normal Django forms.
  17. Uploaded files are stored in GridFS using the mongoengine fields.
  18. Because GridFS has no directories and stores files in a flat space an
  19. uploaded file whose name already exists gets a unique filename with the
  20. form ``<filename>_<unique_number>.<extension>``.
  21. Container fields
  22. ~~~~~~~~~~~~~~~~
  23. For container fields like ``ListFields`` and ``MapFields`` a very simple
  24. widget is used. The widget renders the container content in the
  25. appropriate field plus one empty field. This is mainly done to not
  26. introduce any Javascript dependencies, the backend code will happily
  27. handle any kind of dynamic form, as long as the field ids are
  28. continuously numbered in the POST data.
  29. You can use any of the other supported fields inside list or map fields.
  30. Including ``FileFields`` which aren't really supported by mongoengine
  31. inside container fields.
  32. Usage
  33. -----
  34. mongodbforms supports forms for normal documents and embedded documents.
  35. Normal documents
  36. ~~~~~~~~~~~~~~~~
  37. To use mongodbforms with normal documents replace djangos forms with
  38. mongodbform forms.
  39. .. code:: python
  40. from mongodbforms import DocumentForm
  41. class BlogForm(DocumentForm)
  42. ...
  43. Embedded documents
  44. ~~~~~~~~~~~~~~~~~~
  45. For embedded documents use ``EmbeddedDocumentForm``. The Meta-object of
  46. the form has to be provided with an embedded field name. The embedded
  47. object is appended to this. The form constructor takes a couple of
  48. additional arguments: The document the embedded document gets added to
  49. and an optional position argument.
  50. If no position is provided the form adds a new embedded document to the
  51. list if the form is saved. To edit an embedded document stored in a list
  52. field the position argument is required. If you provide a position and
  53. no instance to the form the instance is automatically loaded using the
  54. position argument.
  55. If the embedded field is a plain embedded field the current object is
  56. simply overwritten.
  57. .. code:: python
  58. # forms.py
  59. from mongodbforms import EmbeddedDocumentForm
  60. class MessageForm(EmbeddedDocumentForm):
  61. class Meta:
  62. document = Message
  63. embedded_field_name = 'messages'
  64. fields = ['subject', 'sender', 'message',]
  65. # views.py
  66. # create a new embedded object
  67. form = MessageForm(parent_document=some_document, ...)
  68. # edit the 4th embedded object
  69. form = MessageForm(parent_document=some_document, position=3, ...)
  70. Documentation
  71. -------------
  72. In theory the documentation `Django's
  73. modelform <https://docs.djangoproject.com/en/dev/topics/forms/modelforms/>`__
  74. documentation should be all you need (except for one exception; read
  75. on). If you find a discrepancy between something that mongodbforms does
  76. and what Django's documentation says, you have most likely found a bug.
  77. Please `report
  78. it <https://github.com/jschrewe/django-mongodbforms/issues>`__.
  79. Form field generation
  80. ~~~~~~~~~~~~~~~~~~~~~
  81. Because the fields on mongoengine documents have no notion of form
  82. fields mongodbform uses a generator class to generate the form field for
  83. a db field, which is not explicitly set.
  84. To use your own field generator you can either set a generator for your
  85. whole project using ``MONGODBFORMS_FIELDGENERATOR`` in settings.py or
  86. you can use the ``formfield_generator`` option on the form's Meta class.
  87. The default generator is defined in ``mongodbforms/fieldgenerator.py``
  88. and should make it easy to override form fields and widgets. If you set
  89. a generator on the document form you can also pass two dicts
  90. ``field_overrides`` and ``widget_overrides`` to ``__init__``. For a list
  91. of valid keys have a look at ``MongoFormFieldGenerator``.
  92. .. code:: python
  93. # settings.py
  94. # set the fieldgeneretor for the whole application
  95. MONGODBFORMS_FIELDGENERATOR = 'myproject.fieldgenerator.GeneratorClass'
  96. # generator.py
  97. from mongodbforms.fieldgenerator import MongoFormFieldGenerator
  98. class MyFieldGenerator(MongoFormFieldGenerator):
  99. ...
  100. # forms.py
  101. from mongodbforms import DocumentForm
  102. from generator import MyFieldGenerator
  103. class MessageForm(DocumentForm):
  104. class Meta:
  105. formfield_generator = MyFieldGenerator