forked from jschrewe/django-mongodbforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.txt
147 lines (105 loc) · 4.63 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
django mongodbforms
===================
This is an implementation of django's model forms for mongoengine
documents.
Requirements
------------
- Django >= 1.4
- `mongoengine <http://mongoengine.org/>`__ >= 0.8.3
Supported field types
---------------------
Mongodbforms supports all the fields that have a simple representation
in Django's formfields (IntField, TextField, etc). In addition it also
supports ``ListFields`` and ``MapFields``.
File fields
~~~~~~~~~~~
Mongodbforms handles file uploads just like the normal Django forms.
Uploaded files are stored in GridFS using the mongoengine fields.
Because GridFS has no directories and stores files in a flat space an
uploaded file whose name already exists gets a unique filename with the
form ``<filename>_<unique_number>.<extension>``.
Container fields
~~~~~~~~~~~~~~~~
For container fields like ``ListFields`` and ``MapFields`` a very simple
widget is used. The widget renders the container content in the
appropriate field plus one empty field. This is mainly done to not
introduce any Javascript dependencies, the backend code will happily
handle any kind of dynamic form, as long as the field ids are
continuously numbered in the POST data.
You can use any of the other supported fields inside list or map fields.
Including ``FileFields`` which aren't really supported by mongoengine
inside container fields.
Usage
-----
mongodbforms supports forms for normal documents and embedded documents.
Normal documents
~~~~~~~~~~~~~~~~
To use mongodbforms with normal documents replace djangos forms with
mongodbform forms.
.. code:: python
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 a couple of
additional arguments: The document the embedded document gets added to
and an optional position argument.
If no position is provided the form adds a new embedded document to the
list if the form is saved. To edit an embedded document stored in a list
field the position argument is required. If you provide a position and
no instance to the form the instance is automatically loaded using the
position argument.
If the embedded field is a plain embedded field the current object is
simply overwritten.
.. code:: python
# forms.py
from mongodbforms import EmbeddedDocumentForm
class MessageForm(EmbeddedDocumentForm):
class Meta:
document = Message
embedded_field_name = 'messages'
fields = ['subject', 'sender', 'message',]
# views.py
# create a new embedded object
form = MessageForm(parent_document=some_document, ...)
# edit the 4th embedded object
form = MessageForm(parent_document=some_document, position=3, ...)
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 mongodbform uses a generator class to generate the form field for
a db field, which is not explicitly set.
To use your own field generator you can either set a generator for your
whole project using ``MONGODBFORMS_FIELDGENERATOR`` in settings.py or
you can use the ``formfield_generator`` option on the form's Meta class.
The default generator is defined in ``mongodbforms/fieldgenerator.py``
and should make it easy to override form fields and widgets. If you set
a generator on the document form you can also pass two dicts
``field_overrides`` and ``widget_overrides`` to ``__init__``. For a list
of valid keys have a look at ``MongoFormFieldGenerator``.
.. code:: python
# settings.py
# set the fieldgeneretor for the whole application
MONGODBFORMS_FIELDGENERATOR = 'myproject.fieldgenerator.GeneratorClass'
# 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