Internal-only factory for constructing model instances with pk_only or excluded support. Not reachable from user-supplied kwargs or JSON deserialization. :param _pk_only: if True, skip validation and set only pk field :type _pk_only: bool :param _exc
(
cls,
_pk_only: bool = False,
_excluded: Optional[set[str]] = None,
**kwargs: Any,
)
| 162 | |
| 163 | @classmethod |
| 164 | def _internal_construct( |
| 165 | cls, |
| 166 | _pk_only: bool = False, |
| 167 | _excluded: Optional[set[str]] = None, |
| 168 | **kwargs: Any, |
| 169 | ) -> "NewBaseModel": |
| 170 | """ |
| 171 | Internal-only factory for constructing model instances with pk_only or |
| 172 | excluded support. Not reachable from user-supplied kwargs or JSON |
| 173 | deserialization. |
| 174 | |
| 175 | :param _pk_only: if True, skip validation and set only pk field |
| 176 | :type _pk_only: bool |
| 177 | :param _excluded: set of field names to explicitly set to None |
| 178 | :type _excluded: Optional[set[str]] |
| 179 | :param kwargs: field values for the model |
| 180 | :type kwargs: Any |
| 181 | :return: constructed model instance |
| 182 | :rtype: NewBaseModel |
| 183 | """ |
| 184 | instance = cls.__new__(cls) |
| 185 | instance._verify_model_can_be_initialized() |
| 186 | instance._initialize_internal_attributes() |
| 187 | object.__setattr__(instance, "__pk_only__", _pk_only) |
| 188 | object.__setattr__(instance, "__ormar_excludable__", None) |
| 189 | |
| 190 | new_kwargs, through_tmp_dict = instance._process_kwargs(kwargs) |
| 191 | |
| 192 | if _excluded: |
| 193 | for field_to_nullify in _excluded: |
| 194 | new_kwargs[field_to_nullify] = None |
| 195 | |
| 196 | if not _pk_only: |
| 197 | new_kwargs = instance.serialize_nested_models_json_fields(new_kwargs) |
| 198 | instance.__pydantic_validator__.validate_python( |
| 199 | new_kwargs, |
| 200 | self_instance=instance, # type: ignore |
| 201 | ) |
| 202 | else: |
| 203 | fields_set = {instance.ormar_config.pkname} |
| 204 | object.__setattr__(instance, "__dict__", new_kwargs) |
| 205 | object.__setattr__(instance, "__pydantic_fields_set__", fields_set) |
| 206 | |
| 207 | instance._register_related_models(new_kwargs, through_tmp_dict) |
| 208 | return instance |
| 209 | |
| 210 | def _register_related_models( |
| 211 | self, new_kwargs: dict[str, Any], through_tmp_dict: dict[str, Any] |