MCPcopy Create free account
hub / github.com/MongoEngine/mongoengine / _lookup_field

Method _lookup_field

mongoengine/base/document.py:1064–1192  ·  view source on GitHub ↗

Given the path to a given field, return a list containing the Field object associated with that field and all of its parent Field objects. Args: parts (str, list, or tuple) - path to the field. Should be a string for simple fields existing on this doc

(cls, parts)

Source from the content-addressed store, hash-verified

1062
1063 @classmethod
1064 def _lookup_field(cls, parts):
1065 """Given the path to a given field, return a list containing
1066 the Field object associated with that field and all of its parent
1067 Field objects.
1068
1069 Args:
1070 parts (str, list, or tuple) - path to the field. Should be a
1071 string for simple fields existing on this document or a list
1072 of strings for a field that exists deeper in embedded documents.
1073
1074 Returns:
1075 A list of Field instances for fields that were found or
1076 strings for sub-fields that weren't.
1077
1078 Example:
1079 >>> user._lookup_field('name')
1080 [<mongoengine.fields.StringField at 0x1119bff50>]
1081
1082 >>> user._lookup_field('roles')
1083 [<mongoengine.fields.EmbeddedDocumentListField at 0x1119ec250>]
1084
1085 >>> user._lookup_field(['roles', 'role'])
1086 [<mongoengine.fields.EmbeddedDocumentListField at 0x1119ec250>,
1087 <mongoengine.fields.StringField at 0x1119ec050>]
1088
1089 >>> user._lookup_field('doesnt_exist')
1090 raises LookUpError
1091
1092 >>> user._lookup_field(['roles', 'doesnt_exist'])
1093 [<mongoengine.fields.EmbeddedDocumentListField at 0x1119ec250>,
1094 'doesnt_exist']
1095
1096 """
1097 # TODO this method is WAY too complicated. Simplify it.
1098 # TODO don't think returning a string for embedded non-existent fields is desired
1099
1100 ListField = _import_class("ListField")
1101 DynamicField = _import_class("DynamicField")
1102
1103 if not isinstance(parts, (list, tuple)):
1104 parts = [parts]
1105
1106 fields = []
1107 field = None
1108
1109 for field_name in parts:
1110 # Handle ListField indexing:
1111 if field_name.isdigit() and isinstance(field, ListField):
1112 fields.append(field_name)
1113 continue
1114
1115 # Look up first field from the document
1116 if field is None:
1117 if field_name == "pk":
1118 # Deal with "primary key" alias
1119 field_name = cls._meta["id_field"]
1120
1121 if field_name in cls._fields:

Callers 11

_integrate_shard_keyMethod · 0.80
sumMethod · 0.80
averageMethod · 0.80
_fields_to_dbfieldsMethod · 0.80
field_subMethod · 0.80
field_path_subMethod · 0.80
queryFunction · 0.80
updateFunction · 0.80
_build_index_specMethod · 0.80
_unique_with_indexesMethod · 0.80
_translate_field_nameMethod · 0.80

Calls 8

lookup_memberMethod · 0.95
_import_classFunction · 0.90
LookUpErrorClass · 0.90
DynamicFieldClass · 0.85
joinMethod · 0.80
formatMethod · 0.80
getMethod · 0.45
lookup_memberMethod · 0.45

Tested by

no test coverage detected