I do a code with an abstract model for control create and update documents:
class Control(models.Model):
ctrl_dt_add = models.DateTimeField(auto_now_add=True, auto_now=False)
ctrl_dt_mod = models.DateTimeField(auto_now_add=False, auto_now=True)
ctrl_dt_del = models.DateTimeField(default=None,blank=True,null=True)
ctrl_user_add = models.OneToOneField(
to=User,
on_delete=models.DO_NOTHING,
default=None,
blank=True,
null=True,
)
ctrl_user_mod = models.OneToOneField(
to=User,
on_delete=models.DO_NOTHING,
default=None,
blank=True,
null=True,
)
ctrl_user_del = models.OneToOneField(
to=User,
on_delete=models.DO_NOTHING,
default=None,
blank=True,
null=True,
)
ctrl_deleted = models.BooleanField(default=False)
class Meta:
abstract = True
And my model:
class MyModel(models.Model):
_id = models.ObjectIdField()
..........
control = models.EmbeddedField(
model_container = Control,
model_form_class = ControlForm
)
objects = models.DjongoManager()
class Meta:
db_table = 'mymodel'
def __str__(self):
return self.value
My problem is, when i create a new document with this model, control key insert in all datetime keys a null value like this:
{
"_id" : ObjectId("5f04a3ecc108e12ca4bf5a7f"),
............
"control" : {
"ctrl_dt_add" : null,
"ctrl_dt_mod" : null,
"ctrlwdt_del" : null,
"ctrl_user_add_id" : 1,
"ctrl_user_mod_id" : null,
"ctrl_user_del_id" : null,
"ctrl_deleted" : false
}
}
I need to declare values in all the actions with this model. I save it creating a save function in the model. But i would like to do not need that for work with it.
def save(self, request=None, *args, **kwargs):
if request:
self.control.ctrl_user_mod = request.user
self.control.ctrl_dt_mod = now()
super().save(*args, **kwargs)
Thank you!!