|
| 1 | +django mongodbforms |
| 2 | +=================== |
| 3 | + |
| 4 | +This is an implementation of django's model forms for mongoengine |
| 5 | +documents. |
| 6 | + |
| 7 | +Requirements |
| 8 | +------------ |
| 9 | + |
| 10 | +- `mongoengine <http://mongoengine.org/>`_ |
| 11 | + |
| 12 | +Usage |
| 13 | +----- |
| 14 | + |
| 15 | +mongodbforms supports forms for normal documents and embedded documents. |
| 16 | + |
| 17 | +Normal documents |
| 18 | +~~~~~~~~~~~~~~~~ |
| 19 | + |
| 20 | +To use mongodbforms with normal documents replace djangos forms with |
| 21 | +mongodbform forms. |
| 22 | + |
| 23 | +:: |
| 24 | + |
| 25 | + from mongodbforms import DocumentForm |
| 26 | + |
| 27 | + class BlogForm(DocumentForm) |
| 28 | + ... |
| 29 | + |
| 30 | +Embedded documents |
| 31 | +~~~~~~~~~~~~~~~~~~ |
| 32 | + |
| 33 | +For embedded documents use ``EmbeddedDocumentForm``. The Meta-object of |
| 34 | +the form has to be provided with an embedded field name. The embedded |
| 35 | +object is appended to this. The form constructor takes an additional |
| 36 | +argument: The document the embedded document gets added to. |
| 37 | + |
| 38 | +If the form is saved the new embedded object is automatically added to |
| 39 | +the provided parent document. If the embedded field is a list field the |
| 40 | +embedded document is appended to the list, if it is a plain embedded |
| 41 | +field the current object is overwritten. Note that the parent document |
| 42 | +is not saved. |
| 43 | + |
| 44 | +:: |
| 45 | + |
| 46 | + # forms.py |
| 47 | + from mongodbforms import EmbeddedDocumentForm |
| 48 | + |
| 49 | + class MessageForm(EmbeddedDocumentForm): |
| 50 | + class Meta: |
| 51 | + document = Message |
| 52 | + embedded_field_name = 'messages' |
| 53 | + |
| 54 | + fields = ['subject', 'sender', 'message',] |
| 55 | + |
| 56 | + # views.py |
| 57 | + form = MessageForm(parent_document=some_document, ...) |
| 58 | + |
| 59 | +Documentation |
| 60 | +------------- |
| 61 | + |
| 62 | +In theory the documentation `Django's |
| 63 | +modelform <https://docs.djangoproject.com/en/dev/topics/forms/modelforms/>`_ |
| 64 | +documentation should be all you need (except for one exception; read |
| 65 | +on). If you find a discrepancy between something that mongodbforms does |
| 66 | +and what Django's documentation says, you have most likely found a bug. |
| 67 | +Please `report |
| 68 | +it <https://github.com/jschrewe/django-mongodbforms/issues>`_. |
| 69 | + |
| 70 | +Form field generation |
| 71 | +~~~~~~~~~~~~~~~~~~~~~ |
| 72 | + |
| 73 | +Because the fields on mongoengine documents have no notion of form |
| 74 | +fields every mongodbform uses a generator class to generate the form |
| 75 | +field for a db field, which is not explicitly set. |
| 76 | + |
| 77 | +If you want to use your own generator class you can use the |
| 78 | +``formfield_generator`` option on the form's Meta class. |
| 79 | + |
| 80 | +:: |
| 81 | + |
| 82 | + # generator.py |
| 83 | + from mongodbforms.fieldgenerator import MongoFormFieldGenerator |
| 84 | + |
| 85 | + class MyFieldGenerator(MongoFormFieldGenerator): |
| 86 | + ... |
| 87 | + |
| 88 | + # forms.py |
| 89 | + from mongodbforms import DocumentForm |
| 90 | + |
| 91 | + from generator import MyFieldGenerator |
| 92 | + |
| 93 | + class MessageForm(DocumentForm): |
| 94 | + class Meta: |
| 95 | + formfield_generator = MyFieldGenerator |
| 96 | + |
0 commit comments