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

Method __new__

mongoengine/base/metaclasses.py:252–423  ·  view source on GitHub ↗
(mcs, name, bases, attrs)

Source from the content-addressed store, hash-verified

250 """
251
252 def __new__(mcs, name, bases, attrs):
253 flattened_bases = mcs._get_bases(bases)
254 super_new = super().__new__
255
256 # Set default _meta data if base class, otherwise get user defined meta
257 if attrs.get("my_metaclass") == TopLevelDocumentMetaclass:
258 # defaults
259 attrs["_meta"] = {
260 "abstract": True,
261 "max_documents": None,
262 "max_size": None,
263 "ordering": [], # default ordering applied at runtime
264 "indexes": [], # indexes to be ensured at runtime
265 "id_field": None,
266 "index_background": False,
267 "index_opts": None,
268 "delete_rules": None,
269 # allow_inheritance can be True, False, and None. True means
270 # "allow inheritance", False means "don't allow inheritance",
271 # None means "do whatever your parent does, or don't allow
272 # inheritance if you're a top-level class".
273 "allow_inheritance": None,
274 }
275 attrs["_is_base_cls"] = True
276 attrs["_meta"].update(attrs.get("meta", {}))
277 else:
278 attrs["_meta"] = attrs.get("meta", {})
279 # Explicitly set abstract to false unless set
280 attrs["_meta"]["abstract"] = attrs["_meta"].get("abstract", False)
281 attrs["_is_base_cls"] = False
282
283 # Set flag marking as document class - as opposed to an object mixin
284 attrs["_is_document"] = True
285
286 # Ensure queryset_class is inherited
287 if "objects" in attrs:
288 manager = attrs["objects"]
289 if hasattr(manager, "queryset_class"):
290 attrs["_meta"]["queryset_class"] = manager.queryset_class
291
292 # Clean up top level meta
293 if "meta" in attrs:
294 del attrs["meta"]
295
296 # Find the parent document class
297 parent_doc_cls = [
298 b for b in flattened_bases if b.__class__ == TopLevelDocumentMetaclass
299 ]
300 parent_doc_cls = None if not parent_doc_cls else parent_doc_cls[0]
301
302 # Prevent classes setting collection different to their parents
303 # If parent wasn't an abstract class
304 if (
305 parent_doc_cls
306 and "collection" in attrs.get("_meta", {})
307 and not parent_doc_cls._meta.get("abstract", True)
308 ):
309 msg = "Trying to set a collection on a subclass (%s)" % name

Callers

nothing calls this directly

Calls 11

mergeMethod · 0.95
QuerySetManagerClass · 0.90
ObjectIdFieldClass · 0.90
MetaDictClass · 0.85
_get_basesMethod · 0.80
joinMethod · 0.80
_build_index_specsMethod · 0.80
itemsMethod · 0.80
get_auto_id_namesMethod · 0.80
getMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected