- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Django 2.0
。
我有一个像这样的模型
class MyModel(models.Model):
update_new = models.CharField(blank=True, max_length=200)
modified = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
并使用 Model Manager
更新模型数据
class MyModelManager(models.Manager):
def get_queryset(self):
return MyModelQueryset(self.model, self._db)
def update_or_create(self, pk, **save_data):
record = MyModel.objects.filter(
pk=pk
)
if record.exists():
# setting field manually for testing
save_data['update_new'] = 'anuj'
uc = record.update(**save_data)
print(uc) # prints 1
return record.first(), True
record, created = self.get_queryset().get_or_create(
pk=pk
**save_data
)
return record, created
这工作正常并且值已更新。但是 modified 字段没有更新。 created 和 modified 字段中的值保持不变(记录创建时的时间戳)
最佳答案
docs 中描述了这种行为:
The field is only automatically updated when calling Model.save(). The field isn’t updated when making updates to other fields in other ways such as QuerySet.update(), though you can specify a custom value for the field in an update like that.
关于django - auto_now 字段未使用 filter() 进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52159958/
我有以下型号: class MetaData(models.Model): created_at = models.DateTimeField(auto_now_add=True, auto_
我已经为数据库中的事件定义了一些时间戳作为 auto_now_add,因为在存储事件的同时信息应该与它的时间戳一起存储。 事件的描述是这样的 class NewEvent(models.Model):
我已经设置了我的 django 并且我有一个 auto_now=True 的模型,因为我的 SQL 服务器时区已设置为“亚洲/上海”,所以我也想将 django 设置为这个时区。我已将其设置为 TIM
总的来说,我对 django 和 python 非常陌生,我正在尝试学习rest_framework 来创建 RESTful API。 所以我有一个这样的模型: class Listing(model
我想在保存记录时更新相关模型的时间戳。这是我的模型: class Issue(models.Model): issueTitle = models.CharField() issueD
我有这样一个模型: class FooBar(models.Model): createtime = models.DateTimeField(auto_now_add=True) l
我有一个模型,如下所示: name = models.CharField(max_length=255) modify_time = models.DateTimeField(auto
我正在使用 Django 2.0。 我有一个像这样的模型 class MyModel(models.Model): update_new = models.CharField(blank=Tr
我正在使用 Django 2.0。 我有一个像这样的模型 class MyModel(models.Model): update_new = models.CharField(blank=Tr
我使用last_touch_date DateTimeProperty 作为使用auto_now=True 标志修改应用程序数据存储中的实体的方法。当用户发布实体时,它会收到其last_touch_d
我在Django 模型字段的属性中理解的是 auto_now - 每次调用 Model.save() 时将字段值更新为当前时间和日期。 auto_now_add - 使用创建记录的时间和日期更新值。
对于 Django 1.1。 我的 models.py 中有这个: class User(models.Model): created = models.DateTimeField(auto_
我一直在尝试调试为什么我的几个模型中的 last_updated 字段在执行例如 model.objects.filter(**lookups).update(**defaults) 时没有被更新。例
我有一个 Comment模型。它具有以下时间戳字段: created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
模型定义: class Footprint(models.Model) date = models.DateTimeField(auto_now = True) class Stuff(Footp
我正在使用 Django,但在使用 Django 模型的 Python 脚本时遇到问题我正在使用的脚本从 api 获取数据并将其加载到我的数据库中。 我的模型: class Movie(models.
我有两个模型:消息和附件。每个附件都附加到特定的消息,使用附件模型上的 ForeignKey。两种模型都有一个名为 updated 的 auto_now DateTimeField。我试图做到这一点,
我正在尝试做 django api。 在 models.py 中 class Receipt(models.Model): id=models.AutoField(primary_key=Tr
我使用的是 Django 2.1.5。 有一个带有“auto_now”字段的模型: class BaseModel(models.Model): id = models.UUIDField(d
我需要系统中的每个日期时间都采用 UTC 时间。如果我使用 auto_now,如何确保它将 tzinfo 设置为 UTC 的 python datetime?我想创建一个字段如下: class MyT
我是一名优秀的程序员,十分优秀!