Skip to main content

An implementation of django forms using mongoengine.

Project description

django mongodbforms

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

Requirements

Usage

mongodbforms supports forms for normal documents and embedded documents.

Normal documents

To use mongodbforms with normal documents replace djangos forms with mongodbform forms.

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

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

````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 ```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongodbforms-0.2.1.tar.gz (23.5 kB view details)

Uploaded Source

File details

Details for the file mongodbforms-0.2.1.tar.gz.

File metadata

File hashes

Hashes for mongodbforms-0.2.1.tar.gz
Algorithm Hash digest
SHA256 ab356c48be25e146bdb122c26cd67adec8a3c2665d86924a1671798cbdaf1cad
MD5 1768ea7e6775a36b3c5e71ea7da7840b
BLAKE2b-256 e074d3cb08ae92b4ac360b028aa2d2d329696700c5aa703b3b48762e0df56241

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page