我用的是Django 2.1.5。你知道吗
有一个带有“auto\u now”字段的模型:
class BaseModel(models.Model):
id = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True, unique=True, primary_key=True)
updated_at = models.DateTimeField(db_index=True, auto_now=True)
updated_by = models.CharField(max_length=200)
responded_at = models.DateTimeField(db_index=True, null=True, blank=True)
responded_by = models.CharField(max_length=200, null=True, blank=True)
现在,我有了这个模型的pre_save
信号,我想在那里更新responded_at
和responded_by
字段,使之等于updated_at
和updated_by
。在该信号中,updated_by
值已经是新的值,应该在请求的末尾,但是updated_at
不是。它是旧(当前)值。
如果可能的话,我希望能够在保存之后获得应该在updated_at
字段中的值。你知道吗
我之所以使用pre_save
信号而不是post_save
,是因为我正在更新其中的实例。你知道吗
因为您的
auto_now
与updated_at
字段一起使用,所以它将继承editable=False
和blank=True
。你知道吗作为docs状态:
为了避免这种情况,可以编写如下自定义字段:
您可以在
BaseModel
中这样使用它:这样
updated_at
字段应该是可编辑的,您的信号应该可以工作。你知道吗相关问题 更多 >
编程相关推荐