Skip to main content

This is an implementation of Django’s model forms for mongoengine documents.

This a fork of Jan 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.

Requirements

Earlier versions may work, but we’re currently not testing with those.

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.

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.

# 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 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.

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.

# 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

Release files for django-mongoengine-forms 0.4.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-mongoengine-forms 0.4.6
File Size Uploaded
django-mongoengine-forms-0.4.6.tar.gz 29.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-mongoengine-forms 0.4.6
File Interpreter ABI Platform
django_mongoengine_forms-0.4.6-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 57.6 kB

Release files / django-mongoengine-forms-0.4.6.tar.gz

Download URL django-mongoengine-forms-0.4.6.tar.gz
Size 29.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a632f680d464748c76d2e5cc51e2ab92a7f5b04d373d5854339f2a1da656827a
BLAKE2b-256 checksum
How to use checksums
605e12c9e4747a3c8feeacebc94f99ab719df12ec639f730465e571f02e85863
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_mongoengine_forms-0.4.6-py2.py3-none-any.whl

Download URL django_mongoengine_forms-0.4.6-py2.py3-none-any.whl
Size 28.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b9901950aade763ebcdd8c7abd323517cf55aade3614b3078fb7401616b67d1a
BLAKE2b-256 checksum
How to use checksums
e0be61de3b1a7d19d6cd67f364359e24d4fa5d87435895225f8c709970e0127b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.4.6 This release

2 release files

0.4.5

2 release files

0.4.4

3 release files

0.4.3

3 release files

0.4.2

3 release files

0.4.1

2 release files

0.4.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page