service
django_spire.contrib.service
__all__ = ['BaseDjangoModelService', 'ServiceError']
module-attribute
ServiceError
Bases: DjangoSpireError
BaseDjangoModelService
Bases: BaseDjangoModelConstructor[TypeDjangoModel_co], ABC, Generic[TypeDjangoModel_co]
Source code in django_spire/contrib/constructor/constructor.py
save_model_obj
This is the core service method for saving a Django model object with field data provided via kwargs.
It will update the object with the given kwargs and handle any upstream attribute changes that were applied
directly to the model instance (i.e. it can also be called without any kwargs, similar to Model.save()).
Its purpose is to run extra operations related to the model instance that need to run each time
the model is saved - it is meant to replace the need for overriding BaseModelForm.save() or Model.save().
It is designed to emulate django's BaseModelForm.save() method as close as possible:
first, in _set_non_m2m_fields, it updates the fields on self.obj using logic similar to django.forms.models.construct_instance,
then it calls self.obj.save(), then updates the m2m fields on the object instance
using logic similar to django's BaseModelForm._save_m2m() method. In all cases,
it treats the incoming field_data exactly the same as cleaned_data is treated
in the django code that it is emulating, and therefore does not perform any validation
on the data or the model instance as it is assumed that field_data has already been validated upstream.
Args: **field_data:
Returns: tuple[Model, bool]