Skip to content
This repository
Browse code

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
5 mongoalchemy/document.py
@@ -147,13 +147,12 @@ class Document(object):
147 147 ''' Use the base class collection name for the subclasses. Default: False
148 148 '''
149 149
150   -
151 150 config_polymorphic_identity = None
152 151 ''' When using a string value with ``config_polymorphic_on`` in a parent
153 152 class, this is the value that the attribute is compared to when
154 153 determining
155 154 '''
156   -
  155 +
157 156 config_full_name = None
158 157 ''' If namespaces are being used, the key for a class is normally
159 158 the class name. In some cases the same class name may be used in
@@ -251,7 +250,7 @@ def get_subclass(cls, obj):
251 250 return sub_value
252 251 def __eq__(self, other):
253 252 try:
254   - self.mongo_id == other.mongo_id
  253 + return self.mongo_id == other.mongo_id
255 254 except:
256 255 return False
257 256
13 mongoalchemy/fields.py
@@ -856,9 +856,18 @@ class ObjectIdField(Field):
856 856
857 857 valid_modifiers = SCALAR_MODIFIERS
858 858
859   - def __init__(self, session=None, **kwargs):
  859 + def __init__(self, session=None, auto=False, **kwargs):
860 860 super(ObjectIdField, self).__init__(**kwargs)
861   -
  861 + self.auto = auto
  862 +
  863 + def set_default(self, value):
  864 + self._default = value
  865 + def get_default(self):
  866 + if self.auto:
  867 + self._default = ObjectId()
  868 + return self._default
  869 + default = property(get_default, set_default)
  870 +
862 871 def gen(self):
863 872 return ObjectId()
864 873
2  mongoalchemy/session.py
@@ -355,13 +355,11 @@ def flush(self, safe=None):
355 355 self.clear()
356 356
357 357 def dereference(self, ref):
358   - print 'DEREF', ref
359 358 if isinstance(ref, Document):
360 359 return ref
361 360 assert hasattr(ref, 'type')
362 361
363 362 obj = self.cache_read(ref.id)
364   - print 'CACHE READ', obj
365 363 if obj is not None:
366 364 return obj
367 365 value = self.db.dereference(ref)
6 test/test_fields.py
@@ -192,6 +192,12 @@ def objectid_wrong_type_unwrap_test():
192 192 from bson.objectid import ObjectId
193 193 ObjectIdField().unwrap(1)
194 194
  195 +def test_object_id_auto():
  196 + from mongoalchemy.document import Document
  197 + class A(Document):
  198 + idf = ObjectIdField(auto=True, required=False)
  199 + assert 'idf' in A().wrap()
  200 +
195 201 #ObjectID Field
196 202 @raises(BadValueException)
197 203 def objectid_wrong_hex_length_test():

0 comments on commit 3e51d00

Please sign in to comment.
Something went wrong with that request. Please try again.