Skip to main content

An implementation of django forms using mongoengine.

Project description

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.

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

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.1.2.tar.gz (12.7 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for mongodbforms-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b9817629bd7d494fd2554f1bf2e52b1a68dc06d6d1fdef55ffcfc202705bc847
MD5 9f89ac6b58970a9db8d6d61a6df16d91
BLAKE2b-256 2a3a7533b685db76148d7ace1f0a7c1169fe8142d0b73a080ed999d7a13a8c26

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