In our project we use soft delete (is_deleted attribute on a model). Before creating a new model I want to first check if it was already exist and deleted and if so - set is_deleted=False and save the already exists model. So I decided to override the model 'save' method. (not the View Create since it should work not only for requests) The problem is that 'save()' is called for all actions (create, update, delete). Is there a way I can call the overridden method only on create?
class Model(BaseModel): title = models.CharField(max_length=200) identifier = models.CharField(max_length=15) def save(self, *args, **kwargs): try: existing_model = Model.objects.active_and_deleted().get( identifier=self.identifier) if self.is_deleted is False: existing_model.is_deleted = False existing_model.title = self.title existing_model.created_at = self.created_at existing_model.updated_at = self.updated_at existing_model.created_by = self.created_by existing_model.deleted_at = None super(Model, existing_model).save(args, kwargs) except Model.DoesNotExist: # in case the Nodel wasn't already exist - continue pass super(Model, self).save(*args, **kwargs)
is_deleted=False. when someone deletes something, you just turnis_deletedto True. That's right?title