Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow "auto" _id so that references can be made before inserting in t…

…he database
  • Loading branch information...
commit 3e51d006bfaa0aa4d7604dfc2ce0d6b4296ef91c 1 parent 6bf8891
Jeff Jenkins authored
View
5 mongoalchemy/document.py
@@ -147,13 +147,12 @@ class Document(object):
''' Use the base class collection name for the subclasses. Default: False
'''
-
config_polymorphic_identity = None
''' When using a string value with ``config_polymorphic_on`` in a parent
class, this is the value that the attribute is compared to when
determining
'''
-
+
config_full_name = None
''' If namespaces are being used, the key for a class is normally
the class name. In some cases the same class name may be used in
@@ -251,7 +250,7 @@ def get_subclass(cls, obj):
return sub_value
def __eq__(self, other):
try:
- self.mongo_id == other.mongo_id
+ return self.mongo_id == other.mongo_id
except:
return False
View
13 mongoalchemy/fields.py
@@ -856,9 +856,18 @@ class ObjectIdField(Field):
valid_modifiers = SCALAR_MODIFIERS
- def __init__(self, session=None, **kwargs):
+ def __init__(self, session=None, auto=False, **kwargs):
super(ObjectIdField, self).__init__(**kwargs)
-
+ self.auto = auto
+
+ def set_default(self, value):
+ self._default = value
+ def get_default(self):
+ if self.auto:
+ self._default = ObjectId()
+ return self._default
+ default = property(get_default, set_default)
+
def gen(self):
return ObjectId()
View
2  mongoalchemy/session.py
@@ -355,13 +355,11 @@ def flush(self, safe=None):
self.clear()
def dereference(self, ref):
- print 'DEREF', ref
if isinstance(ref, Document):
return ref
assert hasattr(ref, 'type')
obj = self.cache_read(ref.id)
- print 'CACHE READ', obj
if obj is not None:
return obj
value = self.db.dereference(ref)
View
6 test/test_fields.py
@@ -192,6 +192,12 @@ def objectid_wrong_type_unwrap_test():
from bson.objectid import ObjectId
ObjectIdField().unwrap(1)
+def test_object_id_auto():
+ from mongoalchemy.document import Document
+ class A(Document):
+ idf = ObjectIdField(auto=True, required=False)
+ assert 'idf' in A().wrap()
+
#ObjectID Field
@raises(BadValueException)
def objectid_wrong_hex_length_test():
Please sign in to comment.
Something went wrong with that request. Please try again.