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