MCPcopy
hub / github.com/ormar-orm/ormar / populate_default_values

Method populate_default_values

ormar/models/mixins/save_mixin.py:237–257  ·  view source on GitHub ↗

Receives dictionary of model that is about to be saved and populates the default value on the fields that have the default value set, but no actual value was passed by the user. :param new_kwargs: dictionary of model that is about to be saved :type new_kwarg

(cls, new_kwargs: dict)

Source from the content-addressed store, hash-verified

235
236 @classmethod
237 def populate_default_values(cls, new_kwargs: dict) -> dict:
238 """
239 Receives dictionary of model that is about to be saved and populates the default
240 value on the fields that have the default value set, but no actual value was
241 passed by the user.
242
243 :param new_kwargs: dictionary of model that is about to be saved
244 :type new_kwargs: dict
245 :return: dictionary of model that is about to be saved
246 :rtype: dict
247 """
248 for field_name, field in cls.ormar_config.model_fields.items():
249 if field_name not in new_kwargs and field.has_default(use_server=False):
250 new_kwargs[field_name] = field.get_default()
251 # clear fields with server_default set as None
252 if (
253 field.server_default is not None
254 and new_kwargs.get(field_name, None) is None
255 ):
256 new_kwargs.pop(field_name, None)
257 return new_kwargs
258
259 @classmethod
260 def populate_onupdate_value(

Callers 3

saveMethod · 0.80
prepare_model_to_saveMethod · 0.80

Calls 4

has_defaultMethod · 0.80
get_defaultMethod · 0.80
popMethod · 0.80
getMethod · 0.45

Tested by 1