Merge pull request #2413 from bagerard/dynamic_document_parsing_known_fields
Bug fix in DynamicDocument which is not parsing known fields
This commit is contained in:
commit
eb56fb9bda
@ -6,6 +6,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).
|
||||||
|
- Bug fix in DynamicDocument which isn not parsing known fields in constructor like Document do #2412
|
||||||
- When using pymongo >= 3.7, make use of Collection.count_documents instead of Collection.count
|
- When using pymongo >= 3.7, make use of Collection.count_documents instead of Collection.count
|
||||||
and Cursor.count that got deprecated in pymongo >= 3.7.
|
and Cursor.count that got deprecated in pymongo >= 3.7.
|
||||||
This should have a negative impact on performance of count see Issue #2219
|
This should have a negative impact on performance of count see Issue #2219
|
||||||
|
@ -111,25 +111,22 @@ class BaseDocument:
|
|||||||
if "_cls" not in values:
|
if "_cls" not in values:
|
||||||
self._cls = self._class_name
|
self._cls = self._class_name
|
||||||
|
|
||||||
# Set passed values after initialisation
|
# Set actual values
|
||||||
if self._dynamic:
|
dynamic_data = {}
|
||||||
dynamic_data = {}
|
FileField = _import_class("FileField")
|
||||||
for key, value in values.items():
|
for key, value in values.items():
|
||||||
if key in self._fields or key == "_id":
|
key = self._reverse_db_field_map.get(key, key)
|
||||||
setattr(self, key, value)
|
field = self._fields.get(key)
|
||||||
else:
|
if field or key in ("id", "pk", "_cls"):
|
||||||
|
if __auto_convert and value is not None:
|
||||||
|
if field and not isinstance(field, FileField):
|
||||||
|
value = field.to_python(value)
|
||||||
|
setattr(self, key, value)
|
||||||
|
else:
|
||||||
|
if self._dynamic:
|
||||||
dynamic_data[key] = value
|
dynamic_data[key] = value
|
||||||
else:
|
|
||||||
FileField = _import_class("FileField")
|
|
||||||
for key, value in values.items():
|
|
||||||
key = self._reverse_db_field_map.get(key, key)
|
|
||||||
if key in self._fields or key in ("id", "pk", "_cls"):
|
|
||||||
if __auto_convert and value is not None:
|
|
||||||
field = self._fields.get(key)
|
|
||||||
if field and not isinstance(field, FileField):
|
|
||||||
value = field.to_python(value)
|
|
||||||
setattr(self, key, value)
|
|
||||||
else:
|
else:
|
||||||
|
# For strict Document
|
||||||
self._data[key] = value
|
self._data[key] = value
|
||||||
|
|
||||||
# Set any get_<field>_display methods
|
# Set any get_<field>_display methods
|
||||||
|
@ -37,6 +37,19 @@ class TestDynamicDocument(MongoDBTestCase):
|
|||||||
# Confirm no changes to self.Person
|
# Confirm no changes to self.Person
|
||||||
assert not hasattr(self.Person, "age")
|
assert not hasattr(self.Person, "age")
|
||||||
|
|
||||||
|
def test_dynamic_document_parse_values_in_constructor_like_document_do(self):
|
||||||
|
class ProductDynamicDocument(DynamicDocument):
|
||||||
|
title = StringField()
|
||||||
|
price = FloatField()
|
||||||
|
|
||||||
|
class ProductDocument(Document):
|
||||||
|
title = StringField()
|
||||||
|
price = FloatField()
|
||||||
|
|
||||||
|
product = ProductDocument(title="Blabla", price="12.5")
|
||||||
|
dyn_product = ProductDynamicDocument(title="Blabla", price="12.5")
|
||||||
|
assert product.price == dyn_product.price == 12.5
|
||||||
|
|
||||||
def test_change_scope_of_variable(self):
|
def test_change_scope_of_variable(self):
|
||||||
"""Test changing the scope of a dynamic field has no adverse effects"""
|
"""Test changing the scope of a dynamic field has no adverse effects"""
|
||||||
p = self.Person()
|
p = self.Person()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user