MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / __get__

Method __get__

mongoengine/base/fields.py:345–402  ·  view source on GitHub ↗

Descriptor to automatically dereference references.

(self, instance, owner)

Source from the content-addressed store, hash-verified

343 return super().__set__(instance, value)
344
345 def __get__(self, instance, owner):
346 """Descriptor to automatically dereference references."""
347 if instance is None:
348 # Document class being used rather than a document object
349 return self
350
351 ReferenceField = _import_class("ReferenceField")
352 GenericReferenceField = _import_class("GenericReferenceField")
353 EmbeddedDocumentListField = _import_class("EmbeddedDocumentListField")
354
355 auto_dereference = instance._fields[self.name]._auto_dereference
356
357 dereference = auto_dereference and (
358 self.field is None
359 or isinstance(self.field, (GenericReferenceField, ReferenceField))
360 )
361
362 if (
363 instance._initialised
364 and dereference
365 and instance._data.get(self.name)
366 and not getattr(instance._data[self.name], "_dereferenced", False)
367 ):
368 ref_values = instance._data.get(self.name)
369 instance._data[self.name] = self._lazy_load_refs(
370 ref_values=ref_values, instance=instance, name=self.name, max_depth=1
371 )
372 if hasattr(instance._data[self.name], "_dereferenced"):
373 instance._data[self.name]._dereferenced = True
374
375 value = super().__get__(instance, owner)
376
377 # Convert lists / values so we can watch for any changes on them
378 if isinstance(value, (list, tuple)):
379 if issubclass(type(self), EmbeddedDocumentListField) and not isinstance(
380 value, EmbeddedDocumentList
381 ):
382 value = EmbeddedDocumentList(value, instance, self.name)
383 elif not isinstance(value, BaseList):
384 value = BaseList(value, instance, self.name)
385 instance._data[self.name] = value
386 elif isinstance(value, dict) and not isinstance(value, BaseDict):
387 value = BaseDict(value, instance, self.name)
388 instance._data[self.name] = value
389
390 if (
391 auto_dereference
392 and instance._initialised
393 and isinstance(value, (BaseList, BaseDict))
394 and not value._dereferenced
395 ):
396 value = self._lazy_load_refs(
397 ref_values=value, instance=instance, name=self.name, max_depth=1
398 )
399 value._dereferenced = True
400 instance._data[self.name] = value
401
402 return value

Callers

nothing calls this directly

Calls 7

_lazy_load_refsMethod · 0.95
_import_classFunction · 0.90
BaseListClass · 0.90
BaseDictClass · 0.90
getMethod · 0.45
__get__Method · 0.45

Tested by

no test coverage detected