Merge pull request #1951 from bagerard/fix_cls_in_constructor

Regression bug fix - _cls not set in constructor
This commit is contained in:
erdenezul 2018-11-14 17:02:17 +08:00 committed by GitHub
commit 23cc9f6ff8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 4 deletions

View File

@ -5,6 +5,7 @@ Changelog
Development Development
=========== ===========
- (Fill this out as you fix issues and develop your features). - (Fill this out as you fix issues and develop your features).
- Fix `_cls` that is not set properly in Document constructor (regression) #1950
- Fix bug in _delta method - Update of a ListField depends on an unrelated dynamic field update #1733 - Fix bug in _delta method - Update of a ListField depends on an unrelated dynamic field update #1733
- Remove deprecated `save()` method and used `insert_one()` #1899 - Remove deprecated `save()` method and used `insert_one()` #1899

View File

@ -91,6 +91,9 @@ class BaseDocument(object):
value = getattr(self, key, None) value = getattr(self, key, None)
setattr(self, key, value) setattr(self, key, value)
if '_cls' not in values:
self._cls = self._class_name
# Set passed values after initialisation # Set passed values after initialisation
if self._dynamic: if self._dynamic:
dynamic_data = {} dynamic_data = {}

View File

@ -2,11 +2,11 @@
import unittest import unittest
import warnings import warnings
from tests.fixtures import Base from mongoengine import (BooleanField, Document, EmbeddedDocument,
EmbeddedDocumentField, GenericReferenceField,
from mongoengine import Document, EmbeddedDocument, connect, ReferenceField,\ IntField, ReferenceField, StringField, connect)
BooleanField, GenericReferenceField, IntField, StringField
from mongoengine.connection import get_db from mongoengine.connection import get_db
from tests.fixtures import Base
__all__ = ('InheritanceTest', ) __all__ = ('InheritanceTest', )
@ -23,6 +23,27 @@ class InheritanceTest(unittest.TestCase):
continue continue
self.db.drop_collection(collection) self.db.drop_collection(collection)
def test_constructor_cls(self):
# Ensures _cls is properly set during construction
# and when object gets reloaded (prevent regression of #1950)
class EmbedData(EmbeddedDocument):
data = StringField()
meta = {'allow_inheritance': True}
class DataDoc(Document):
name = StringField()
embed = EmbeddedDocumentField(EmbedData)
meta = {'allow_inheritance': True}
test_doc = DataDoc(name='test', embed=EmbedData(data='data'))
assert test_doc._cls == 'DataDoc'
assert test_doc.embed._cls == 'EmbedData'
test_doc.save()
saved_doc = DataDoc.objects.with_id(test_doc.id)
assert test_doc._cls == saved_doc._cls
assert test_doc.embed._cls == saved_doc.embed._cls
test_doc.delete()
def test_superclasses(self): def test_superclasses(self):
"""Ensure that the correct list of superclasses is assembled. """Ensure that the correct list of superclasses is assembled.
""" """