Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def from_query_result(cls, model, doc, nested_doc=False):
parent_key = None
if nested_doc:
doc_dict = doc
elif doc:
parent_key = utils.get_parent_doc(doc.reference.path)
if doc.to_dict():
doc_dict = doc.to_dict()
else:
return None
else:
return None
# instance values is changed according to firestore
# so mark it modified this will help later for figuring
# out the updated fields when need to update this document
setattr(model, '_instance_modified', True)
for k, v in doc_dict.items():
field = model._meta.get_field_by_column_name(k)
# if missing field setting is set to "ignore" then
# get_field_by_column_name return None So, just skip this field
if field is None:
def __init__(self, model_cls, key):
super().__init__(model_cls)
super().set_collection_path(key=key)
self.model = model_cls()
# set parent to this model if any
self.model.parent = utils.get_parent_doc(key)
# Attach key to this model for updating this model
# Purpose of attaching this key is user can update
# this model after getting it
#
# For example:
# u = User.collection.get(user_key)
# u.name = "Updated Name"
# u.update()
self.model._update_doc = key
self.id = utils.get_id(key)
transaction:
Firestore transaction
batch:
Firestore batch writes
"""
# Check doc key is given or not
if key:
self._update_doc = key
# make sure update doc in not None
if self._update_doc is not None and '@temp_doc_id' not in self._update_doc:
# set parent doc from this updated document key
self.parent = utils.get_parent_doc(self._update_doc)
# Get id from key and set it for model
setattr(self, '_id', utils.get_id(self._update_doc))
# Add the temp id field if user is not specified any
if self._id is None and self.id:
setattr(self._meta, 'id', ('id', fields.IDField()))
elif self._update_doc is None and '@temp_doc_id' in self.key:
raise InvalidKey(f'Invalid key to update model "{self.__class__.__name__}" ')
# Get the updated fields
updated_fields = {}
for k, v in self._get_fields().items():
if k in self._field_changed:
updated_fields[k] = v
# Get nested fields if any
# Nested model store as dict in firestore so check values type is dict
if type(v) is dict: