- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用的是 Django 2.1.5。
有一个带有“auto_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状态:
As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set.
为了避免这种情况,您可以编写如下自定义字段:
from django.utils import timezone
class AutoDateTimeField(models.DateTimeField):
def pre_save(self, model_instance, add):
return timezone.now()
您可以在 BaseModel
中使用它,如下所示:
class BaseModel(models.Model):
updated_at = models.AutoDateTimeField(default=timezone.now)
# ...
这样,updated_at
字段应该是可编辑的,并且您的信号应该可以工作。
关于python - Django - 获取 pre_save 信号中的 auto_now 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56872720/
假设我有一个名为 A 的模型,它有一个名为 name 的字段。如何在 pre_save 信号中获取以前的值和新值? @receiver(pre_save, sender=A) def signal_p
我有一个模型类“Action”,它由其他几个类扩展。我是 django 的新手,假设如果我调用 pre_save.connect(actionFunc, sender=Action) ,则只要调用 A
我的模型中有两个必填字段,我想在 ModelViewSet 的 pre_save 方法中填充它们。尽管将它们设置在那里,但在提交 .create() 请求时,我仍然收到一个错误,指出这两个字段是必需的
我正在使用 Django 信号进行数据反规范化。这是我的代码: # vote was saved @receiver(pre_save, sender=Vote) def update_post_vo
我通过以下方式测试了Django的“pre_save”信号,但都无法捕捉到信号。 $ from django.db.models.signals import pre_save import logg
我有一个 Order (1) 和 OrderLine (n) 模型,这里的订单可以有多个订单行。这都是从 Django-admin 内部运行的,其中 OrderLine设置为 inlines 的一部分
如果在处理 instance 中的数据期间出现问题,我想通知用户。在我的 pre_save接收器功能。 是否可以提高自定义ValidationError从接收器功能?如果没有,我将如何实现这样的事情?
以下是我的模型: class Product(models.Model): product_title = models.CharField(max_length=100, null=Fals
我在 MyModel 上定义了一个 pre_save,它看起来像这样: @receiver(pre_save, sender=MyModel) def _mymodel_pre_save(sender
我想在创建用户之前执行一些自定义操作。我想到了为此使用 pre_save 信号。如果其中一个操作会引发异常,则停止事务、中止创建用户等。 这是要走的路吗?如果在此步骤中出现问题(这是必需的行为),它会
我正在使用 Python mongoengine,我想定义回调,每次更新文档时都会调用它。 from mongoengine import * from mongoengine import sign
我正在 Django 1.6 中开发一个通知应用程序,我想将其他参数传递给 Django 信号,例如 post_save .我尝试使用 functools 中的部分,但没有运气。 from funct
当一个字段发生变化时,我需要执行一些操作。 由于此操作需要使用已保存的对象,因此我无法像这样使用 pre_save 信号: @receiver(pre_save, sender=reservation
我必须执行 multi-aspect type of inheritance from UML在 Django ORM 中。我有 Contract 数据类型,根据客户类型(普通客户或商业客户)可以将其
我有一个用于生成 slug 的自定义字段,并且我在我的模型中使用它。 奇怪的是,我无法弄清楚为什么我在此自定义字段的方法 pre_save 中生成的值没有在当前实例上设置。 我的问题不是关于以不同方式
获得 Django 1.11 应用程序。一切工作正常,除了 pre_save 信号出现奇怪的问题。在我的模型中,我有两个多对多字段,用于计算相关模型中的总成本(带宽和许可证)。 我创建了一个 pre_
我有一个模型: class A(models.Model): number = models.IntegerField() 但是当我调用 A.save() 时,我想确保该数字是质数(或其他条件
class TodoList(models.Model): title = models.CharField(maxlength=100) slug = models.SlugFiel
我有两个模型。当我保存第一个实例时,我需要将这个模型的字段值发送到另一个模型的字段中。 第一个模型: class ModelOne(models.Model): # fields...
如果之前已经回答过这个问题但已经搜索了几个小时,我们深表歉意。 我正在尝试对带有 m2m 字段的 django rest 模型进行验证。我有一个分配有角色的模型。如果角色与现有模型重叠并且想抛出验证错
我是一名优秀的程序员,十分优秀!