Skip to content

Commit 915dc53

Browse files
author
Jan Schrewe
committed
Code and docs cleanup.
1 parent e177f83 commit 915dc53

File tree

4 files changed

+13
-109
lines changed

4 files changed

+13
-109
lines changed

mongodbforms/documentoptions.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from django.db.models.fields import FieldDoesNotExist
44
from django.db.models.loading import app_cache_ready
55

6-
from mongoengine.fields import ReferenceField, ObjectIdField
7-
from pymongo.objectid import ObjectId
6+
from mongoengine.fields import ReferenceField
87

98
class PkWrapper(object):
9+
"""
10+
Wraps an immutable class (like mongoengine's pk field) so that attributes can be added.
11+
"""
1012
def __init__(self, baseObject):
1113
self.__class__ = type(baseObject.__class__.__name__, (self.__class__, baseObject.__class__), {})
1214
self.__dict__ = baseObject.__dict__
@@ -15,7 +17,7 @@ def __init__(self, baseObject):
1517
class AdminOptions(object):
1618
"""
1719
Used to store mongoengine's _meta dict to make the document admin
18-
as compatible as possible to the django admin class.
20+
as compatible as possible to django's meta class on models.
1921
"""
2022
def __init__(self, document):
2123
self.index_background = None
@@ -71,6 +73,12 @@ def init_from_meta(self):
7173

7274

7375
def init_pk(self):
76+
"""
77+
Adds a wrapper around the documents pk field. The wrapper object gets the attributes
78+
django expects on the pk field, like name and attname.
79+
80+
The function also adds a _get_pk_val method to the document.
81+
"""
7482
if self.id_field is not None:
7583
try:
7684
pk_field = getattr(self.document, self.id_field)
@@ -148,10 +156,7 @@ def get_field(self, name, many_to_many=True):
148156
"""
149157
Returns the requested field by name. Raises FieldDoesNotExist on error.
150158
"""
151-
for f in self.document._fields.itervalues():
152-
if f.name == name:
153-
return f
154-
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, name))
159+
return self.get_field_by_name(name)
155160

156161
def __getitem__(self, key):
157162
return self.meta[key]

mongodbforms/fields.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

mongodbforms/util.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from mongotools.forms.fields import MongoFormFieldGenerator as MongotoolsGenerator
44

5-
from fields import ReferenceField
65
from documentoptions import AdminOptions
76

87
def init_document_options(document):

readme.md

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This is an implementation of django's model forms for mongoengine documents.
55
## Requirements
66

77
* [mongoengine](http://mongoengine.org/)
8+
* [mongotools](https://github.com/wpjunior/django-mongotools)
89

910
## Usage
1011

@@ -42,52 +43,3 @@ django-mongodbforms currently only supports the most basic things and even they
4243

4344

4445

45-
46-
47-
48-
49-
50-
51-
52-
53-
An implementation of django's model forms for mongoengine documents. I am aware that there is already a [similar project](https://github.com/stephrdev/django-mongoforms), but I needed support for django's `modelform_factory` and `formset_factory`. The code used in this project is mostly taken from django's modelform code.
54-
55-
## Note
56-
57-
**This is pre-alpha software.** Most things are not really tested and although we try to use it wherever posible, you may stumble over weird bugs or your server might explode.
58-
59-
## What works
60-
61-
You should be able to use *mongodbforms* just like django's standard forms. If you can't you have most likely found a bug. [Report it, please.](https://github.com/jschrewe/django-mongodbforms/issues) Thank you.
62-
63-
## Usage
64-
65-
mongodbforms supports forms for normal documents and embedded documents.
66-
67-
### Normal documents
68-
69-
To use mongodbforms with normal documents replace djangos forms with mongodbform forms.
70-
71-
from mongodbforms import DocumentForm
72-
73-
class BlogForm(DocumentForm)
74-
...
75-
76-
### Embedded documents
77-
78-
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.
79-
80-
# forms.py
81-
from mongodbforms import EmbeddedDocumentForm
82-
83-
class MessageForm(EmbeddedDocumentForm):
84-
class Meta:
85-
document = Message
86-
embedded_field_name = 'messages'
87-
88-
fields = ['subject', 'sender', 'message',]
89-
90-
# views.py
91-
form = MessageForm(parent_document=some_document, ...)
92-
93-

0 commit comments

Comments
 (0)