Package: django-mongoengine-forms
This is an implementation of Django’s model forms for mongoengine
documents.This a fork ofJan Schrewe’s
mongodbforms, which
had some issues with keeping stuff up to date and it also carried around
the baggage of supporting ancient Django versions. This fork aims to get
a much leaner version.RequirementsDjango >= 1.9mongoengine>= 0.10.0Earlier versions may work, but we’re currently not testing with those.Supported field typesMongodbforms supports all the fields that have a simple representation
in Django’s formfields (IntField, TextField, etc). In addition it also
supportsListFieldsandMapFields.File fieldsMongodbforms 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 fieldsFor container fields likeListFieldsandMapFieldsa 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.
IncludingFileFieldswhich aren’t really supported by mongoengine
inside container fields.Usagemongodbforms supports forms for normal documents and embedded documents.Normal documentsTo use mongodbforms with normal documents replace djangos forms with
mongodbform forms.frommongodbformsimportDocumentFormclassBlogForm(DocumentForm)...Embedded documentsFor embedded documents useEmbeddedDocumentForm. 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.# forms.pyfrommongodbformsimportEmbeddedDocumentFormclassMessageForm(EmbeddedDocumentForm):classMeta:document=Messageembedded_field_name='messages'fields=['subject','sender','message',]# views.py# create a new embedded objectform=MessageForm(parent_document=some_document,...)# edit the 4th embedded objectform=MessageForm(parent_document=some_document,position=3,...)DocumentationIn theory the documentationDjango’s
modelformdocumentation 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.
Pleasereport
it.Form field generationBecause 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 usingMONGODBFORMS_FIELDGENERATORin settings.py or
you can use theformfield_generatoroption on the form’s Meta class.The default generator is defined inmongodbforms/fieldgenerator.pyand should make it easy to override form fields and widgets. If you set
a generator on the document form you can also pass two dictsfield_overridesandwidget_overridesto__init__. For a list
of valid keys have a look atMongoFormFieldGenerator.# settings.py# set the fieldgeneretor for the whole applicationMONGODBFORMS_FIELDGENERATOR='myproject.fieldgenerator.GeneratorClass'# generator.pyfrommongodbforms.fieldgeneratorimportMongoFormFieldGeneratorclassMyFieldGenerator(MongoFormFieldGenerator):...# forms.pyfrommongodbformsimportDocumentFormfromgeneratorimportMyFieldGeneratorclassMessageForm(DocumentForm):classMeta:formfield_generator=MyFieldGenerator