Merge pull request #2408 from bagerard/refactoring_remove_useless_code_only_fields
Removed code related to Document.__only_fields
This commit is contained in:
commit
161493c0d2
@ -14,6 +14,7 @@ Development
|
|||||||
- Bug fix in ListField when updating the first item, it was saving the whole list, instead of
|
- Bug fix in ListField when updating the first item, it was saving the whole list, instead of
|
||||||
just replacing the first item (as it's usually done) #2392
|
just replacing the first item (as it's usually done) #2392
|
||||||
- Add EnumField: ``mongoengine.fields.EnumField``
|
- Add EnumField: ``mongoengine.fields.EnumField``
|
||||||
|
- Refactoring - Remove useless code related to Document.__only_fields and Queryset.only_fields
|
||||||
|
|
||||||
Changes in 0.20.0
|
Changes in 0.20.0
|
||||||
=================
|
=================
|
||||||
|
@ -64,8 +64,6 @@ class BaseDocument:
|
|||||||
It may contain additional reserved keywords, e.g. "__auto_convert".
|
It may contain additional reserved keywords, e.g. "__auto_convert".
|
||||||
:param __auto_convert: If True, supplied values will be converted
|
:param __auto_convert: If True, supplied values will be converted
|
||||||
to Python-type values via each field's `to_python` method.
|
to Python-type values via each field's `to_python` method.
|
||||||
:param __only_fields: A set of fields that have been loaded for
|
|
||||||
this document. Empty if all fields have been loaded.
|
|
||||||
:param _created: Indicates whether this is a brand new document
|
:param _created: Indicates whether this is a brand new document
|
||||||
or whether it's already been persisted before. Defaults to true.
|
or whether it's already been persisted before. Defaults to true.
|
||||||
"""
|
"""
|
||||||
@ -80,8 +78,6 @@ class BaseDocument:
|
|||||||
|
|
||||||
__auto_convert = values.pop("__auto_convert", True)
|
__auto_convert = values.pop("__auto_convert", True)
|
||||||
|
|
||||||
__only_fields = set(values.pop("__only_fields", values))
|
|
||||||
|
|
||||||
_created = values.pop("_created", True)
|
_created = values.pop("_created", True)
|
||||||
|
|
||||||
signals.pre_init.send(self.__class__, document=self, values=values)
|
signals.pre_init.send(self.__class__, document=self, values=values)
|
||||||
@ -106,10 +102,8 @@ class BaseDocument:
|
|||||||
self._dynamic_fields = SON()
|
self._dynamic_fields = SON()
|
||||||
|
|
||||||
# Assign default values to the instance.
|
# Assign default values to the instance.
|
||||||
# We set default values only for fields loaded from DB. See
|
|
||||||
# https://github.com/mongoengine/mongoengine/issues/399 for more info.
|
|
||||||
for key, field in self._fields.items():
|
for key, field in self._fields.items():
|
||||||
if self._db_field_map.get(key, key) in __only_fields:
|
if self._db_field_map.get(key, key) in values:
|
||||||
continue
|
continue
|
||||||
value = getattr(self, key, None)
|
value = getattr(self, key, None)
|
||||||
setattr(self, key, value)
|
setattr(self, key, value)
|
||||||
@ -758,11 +752,8 @@ class BaseDocument:
|
|||||||
return cls._meta.get("collection", None)
|
return cls._meta.get("collection", None)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _from_son(cls, son, _auto_dereference=True, only_fields=None, created=False):
|
def _from_son(cls, son, _auto_dereference=True, created=False):
|
||||||
"""Create an instance of a Document (subclass) from a PyMongo SON."""
|
"""Create an instance of a Document (subclass) from a PyMongo SON."""
|
||||||
if not only_fields:
|
|
||||||
only_fields = []
|
|
||||||
|
|
||||||
if son and not isinstance(son, dict):
|
if son and not isinstance(son, dict):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"The source SON object needs to be of type 'dict' but a '%s' was found"
|
"The source SON object needs to be of type 'dict' but a '%s' was found"
|
||||||
@ -817,9 +808,7 @@ class BaseDocument:
|
|||||||
if cls.STRICT:
|
if cls.STRICT:
|
||||||
data = {k: v for k, v in data.items() if k in cls._fields}
|
data = {k: v for k, v in data.items() if k in cls._fields}
|
||||||
|
|
||||||
obj = cls(
|
obj = cls(__auto_convert=False, _created=created, **data)
|
||||||
__auto_convert=False, _created=created, __only_fields=only_fields, **data
|
|
||||||
)
|
|
||||||
obj._changed_fields = []
|
obj._changed_fields = []
|
||||||
if not _auto_dereference:
|
if not _auto_dereference:
|
||||||
obj._fields = fields
|
obj._fields = fields
|
||||||
|
@ -88,7 +88,6 @@ class BaseQuerySet:
|
|||||||
self._hint = -1 # Using -1 as None is a valid value for hint
|
self._hint = -1 # Using -1 as None is a valid value for hint
|
||||||
self._collation = None
|
self._collation = None
|
||||||
self._batch_size = None
|
self._batch_size = None
|
||||||
self.only_fields = []
|
|
||||||
self._max_time_ms = None
|
self._max_time_ms = None
|
||||||
self._comment = None
|
self._comment = None
|
||||||
|
|
||||||
@ -190,9 +189,7 @@ class BaseQuerySet:
|
|||||||
if queryset._scalar:
|
if queryset._scalar:
|
||||||
return queryset._get_scalar(
|
return queryset._get_scalar(
|
||||||
queryset._document._from_son(
|
queryset._document._from_son(
|
||||||
queryset._cursor[key],
|
queryset._cursor[key], _auto_dereference=self._auto_dereference,
|
||||||
_auto_dereference=self._auto_dereference,
|
|
||||||
only_fields=self.only_fields,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -200,9 +197,7 @@ class BaseQuerySet:
|
|||||||
return queryset._cursor[key]
|
return queryset._cursor[key]
|
||||||
|
|
||||||
return queryset._document._from_son(
|
return queryset._document._from_son(
|
||||||
queryset._cursor[key],
|
queryset._cursor[key], _auto_dereference=self._auto_dereference,
|
||||||
_auto_dereference=self._auto_dereference,
|
|
||||||
only_fields=self.only_fields,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
raise TypeError("Provide a slice or an integer index")
|
raise TypeError("Provide a slice or an integer index")
|
||||||
@ -719,12 +714,10 @@ class BaseQuerySet:
|
|||||||
|
|
||||||
if full_response:
|
if full_response:
|
||||||
if result["value"] is not None:
|
if result["value"] is not None:
|
||||||
result["value"] = self._document._from_son(
|
result["value"] = self._document._from_son(result["value"])
|
||||||
result["value"], only_fields=self.only_fields
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
if result is not None:
|
if result is not None:
|
||||||
result = self._document._from_son(result, only_fields=self.only_fields)
|
result = self._document._from_son(result)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -757,18 +750,14 @@ class BaseQuerySet:
|
|||||||
docs = self._collection.find({"_id": {"$in": object_ids}}, **self._cursor_args)
|
docs = self._collection.find({"_id": {"$in": object_ids}}, **self._cursor_args)
|
||||||
if self._scalar:
|
if self._scalar:
|
||||||
for doc in docs:
|
for doc in docs:
|
||||||
doc_map[doc["_id"]] = self._get_scalar(
|
doc_map[doc["_id"]] = self._get_scalar(self._document._from_son(doc))
|
||||||
self._document._from_son(doc, only_fields=self.only_fields)
|
|
||||||
)
|
|
||||||
elif self._as_pymongo:
|
elif self._as_pymongo:
|
||||||
for doc in docs:
|
for doc in docs:
|
||||||
doc_map[doc["_id"]] = doc
|
doc_map[doc["_id"]] = doc
|
||||||
else:
|
else:
|
||||||
for doc in docs:
|
for doc in docs:
|
||||||
doc_map[doc["_id"]] = self._document._from_son(
|
doc_map[doc["_id"]] = self._document._from_son(
|
||||||
doc,
|
doc, _auto_dereference=self._auto_dereference,
|
||||||
only_fields=self.only_fields,
|
|
||||||
_auto_dereference=self._auto_dereference,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return doc_map
|
return doc_map
|
||||||
@ -841,7 +830,6 @@ class BaseQuerySet:
|
|||||||
"_collation",
|
"_collation",
|
||||||
"_auto_dereference",
|
"_auto_dereference",
|
||||||
"_search_text",
|
"_search_text",
|
||||||
"only_fields",
|
|
||||||
"_max_time_ms",
|
"_max_time_ms",
|
||||||
"_comment",
|
"_comment",
|
||||||
"_batch_size",
|
"_batch_size",
|
||||||
@ -1045,7 +1033,6 @@ class BaseQuerySet:
|
|||||||
.. versionchanged:: 0.5 - Added subfield support
|
.. versionchanged:: 0.5 - Added subfield support
|
||||||
"""
|
"""
|
||||||
fields = {f: QueryFieldList.ONLY for f in fields}
|
fields = {f: QueryFieldList.ONLY for f in fields}
|
||||||
self.only_fields = list(fields.keys())
|
|
||||||
return self.fields(True, **fields)
|
return self.fields(True, **fields)
|
||||||
|
|
||||||
def exclude(self, *fields):
|
def exclude(self, *fields):
|
||||||
@ -1310,10 +1297,7 @@ class BaseQuerySet:
|
|||||||
def from_json(self, json_data):
|
def from_json(self, json_data):
|
||||||
"""Converts json data to unsaved objects"""
|
"""Converts json data to unsaved objects"""
|
||||||
son_data = json_util.loads(json_data)
|
son_data = json_util.loads(json_data)
|
||||||
return [
|
return [self._document._from_son(data) for data in son_data]
|
||||||
self._document._from_son(data, only_fields=self.only_fields)
|
|
||||||
for data in son_data
|
|
||||||
]
|
|
||||||
|
|
||||||
def aggregate(self, pipeline, *suppl_pipeline, **kwargs):
|
def aggregate(self, pipeline, *suppl_pipeline, **kwargs):
|
||||||
"""Perform a aggregate function based in your queryset params
|
"""Perform a aggregate function based in your queryset params
|
||||||
@ -1638,9 +1622,7 @@ class BaseQuerySet:
|
|||||||
return raw_doc
|
return raw_doc
|
||||||
|
|
||||||
doc = self._document._from_son(
|
doc = self._document._from_son(
|
||||||
raw_doc,
|
raw_doc, _auto_dereference=self._auto_dereference,
|
||||||
_auto_dereference=self._auto_dereference,
|
|
||||||
only_fields=self.only_fields,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._scalar:
|
if self._scalar:
|
||||||
|
@ -3434,7 +3434,7 @@ class TestDocumentInstance(MongoDBTestCase):
|
|||||||
assert obj3 != dbref2
|
assert obj3 != dbref2
|
||||||
assert dbref2 != obj3
|
assert dbref2 != obj3
|
||||||
|
|
||||||
def test_default_values(self):
|
def test_default_values_dont_get_override_upon_save_when_only_is_used(self):
|
||||||
class Person(Document):
|
class Person(Document):
|
||||||
created_on = DateTimeField(default=lambda: datetime.utcnow())
|
created_on = DateTimeField(default=lambda: datetime.utcnow())
|
||||||
name = StringField()
|
name = StringField()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user