fixes for SequenceField
This commit is contained in:
parent
382b9a61a8
commit
130fb9916d
@ -577,6 +577,7 @@ class BaseDocument(object):
|
||||
signals.pre_init.send(self.__class__, document=self, values=values)
|
||||
|
||||
self._data = {}
|
||||
self._initialised = False
|
||||
# Assign default values to instance
|
||||
for attr_name, field in self._fields.items():
|
||||
value = getattr(self, attr_name, None)
|
||||
|
@ -911,14 +911,26 @@ class SequenceField(IntField):
|
||||
|
||||
if instance is None:
|
||||
return self
|
||||
|
||||
if not instance._data:
|
||||
return
|
||||
|
||||
value = instance._data.get(self.name)
|
||||
|
||||
if not value and instance._initialised:
|
||||
value = self.generate_new_value()
|
||||
instance._data[self.name] = value
|
||||
instance._mark_as_changed(self.name)
|
||||
|
||||
return value
|
||||
|
||||
def __set__(self, instance, value):
|
||||
|
||||
if value is None and instance._initialised:
|
||||
value = self.generate_new_value()
|
||||
|
||||
return super(SequenceField, self).__set__(instance, value)
|
||||
|
||||
def to_python(self, value):
|
||||
if value is None:
|
||||
value = self.generate_new_value()
|
||||
|
@ -1425,6 +1425,32 @@ class FieldTest(unittest.TestCase):
|
||||
c = self.db['mongoengine.counters'].find_one({'_id': 'person.id'})
|
||||
self.assertEqual(c['next'], 10)
|
||||
|
||||
def test_sequence_fields_reload(self):
|
||||
class Animal(Document):
|
||||
counter = SequenceField()
|
||||
type = StringField()
|
||||
|
||||
self.db['mongoengine.counters'].drop()
|
||||
Animal.drop_collection()
|
||||
|
||||
a = Animal(type="Boi")
|
||||
a.save()
|
||||
|
||||
self.assertEqual(a.counter, 1)
|
||||
a.reload()
|
||||
self.assertEqual(a.counter, 1)
|
||||
|
||||
a.counter = None
|
||||
self.assertEqual(a.counter, 2)
|
||||
a.save()
|
||||
|
||||
self.assertEqual(a.counter, 2)
|
||||
|
||||
a = Animal.objects.first()
|
||||
self.assertEqual(a.counter, 2)
|
||||
a.reload()
|
||||
self.assertEqual(a.counter, 2)
|
||||
|
||||
def test_multiple_sequence_fields_on_docs(self):
|
||||
|
||||
class Animal(Document):
|
||||
|
Loading…
x
Reference in New Issue
Block a user