Initialise a document or an embedded document. :param values: A dictionary of keys and values for the document. It may contain additional reserved keywords, e.g. "__auto_convert". :param __auto_convert: If True, supplied values will be converted to P
(self, *args, **values)
| 63 | STRICT = False |
| 64 | |
| 65 | def __init__(self, *args, **values): |
| 66 | """ |
| 67 | Initialise a document or an embedded document. |
| 68 | |
| 69 | :param values: A dictionary of keys and values for the document. |
| 70 | It may contain additional reserved keywords, e.g. "__auto_convert". |
| 71 | :param __auto_convert: If True, supplied values will be converted |
| 72 | to Python-type values via each field's `to_python` method. |
| 73 | :param _created: Indicates whether this is a brand new document |
| 74 | or whether it's already been persisted before. Defaults to true. |
| 75 | """ |
| 76 | self._initialised = False |
| 77 | self._created = True |
| 78 | |
| 79 | if args: |
| 80 | raise TypeError( |
| 81 | "Instantiating a document with positional arguments is not " |
| 82 | "supported. Please use `field_name=value` keyword arguments." |
| 83 | ) |
| 84 | |
| 85 | __auto_convert = values.pop("__auto_convert", True) |
| 86 | |
| 87 | _created = values.pop("_created", True) |
| 88 | |
| 89 | signals.pre_init.send(self.__class__, document=self, values=values) |
| 90 | |
| 91 | # Check if there are undefined fields supplied to the constructor, |
| 92 | # if so raise an Exception. |
| 93 | if not self._dynamic and (self._meta.get("strict", True) or _created): |
| 94 | _undefined_fields = set(values.keys()) - set( |
| 95 | list(self._fields.keys()) + ["id", "pk", "_cls", "_text_score"] |
| 96 | ) |
| 97 | if _undefined_fields: |
| 98 | msg = f'The fields "{_undefined_fields}" do not exist on the document "{self._class_name}"' |
| 99 | raise FieldDoesNotExist(msg) |
| 100 | |
| 101 | if self.STRICT and not self._dynamic: |
| 102 | self._data = StrictDict.create(allowed_keys=self._fields_ordered)() |
| 103 | else: |
| 104 | self._data = {} |
| 105 | |
| 106 | self._dynamic_fields = SON() |
| 107 | |
| 108 | # Assign default values for fields |
| 109 | # not set in the constructor |
| 110 | for field_name in self._fields: |
| 111 | if field_name in values: |
| 112 | continue |
| 113 | value = getattr(self, field_name, None) |
| 114 | setattr(self, field_name, value) |
| 115 | |
| 116 | if "_cls" not in values: |
| 117 | self._cls = self._class_name |
| 118 | |
| 119 | # Set actual values |
| 120 | dynamic_data = {} |
| 121 | FileField = _import_class("FileField") |
| 122 | for key, value in values.items(): |
nothing calls this directly
no test coverage detected