Coder Social home page Coder Social logo

laurentpayot / django-mongodbforms Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jschrewe/django-mongodbforms

0.0 3.0 0.0 369 KB

Django forms for mongodb documents

Home Page: http://www.schafproductions.com/projects/django-mongodb-forms/

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

django-mongodbforms's Introduction

django mongodbforms

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

Requirements

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

django-mongodbforms's People

Contributors

audreyfeldroy avatar feliperyan avatar flisky avatar gnublade avatar jmbarbier avatar jschrewe avatar laurentpayot avatar mgerring avatar rafaelnovello avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.