python - “UpdateQuery”对象没有属性 'get_field' 错误

标签 python django

我下面的代码有问题吗?目的是当用户更新 field 时,链接到该 field 的所有事件都被标记为已修改。我收到的错误

"'UpdateQuery' object has no attribute 'get_field' error".

真正奇怪的是,我相信该代码在一个月前经过测试并工作,但突然停止工作。

我的代码:

def save(self, *args, **kwargs):
   super(Venue, self).save(*args, **kwargs)  # Call the "real" save() method.      
   Event.objects.all().filter(venue=self).update(modified=timezone.now())

这个错误是在subqueries.py中抛出的:

def add_update_values(self, values):
     """
     Convert a dictionary of field name to value mappings into an update
     query. This is the entry point for the public update() method on
     querysets.
     """
     values_seq = []
     for name, val in six.iteritems(values):
        field = self.get_field(name)

最佳答案

我不断收到上述错误,但这种手动迭代相关字段并为每个字段调用保存的解决方法有效。在这种情况下,修改后的属性会自动更新,因此只需调用保存即可。出于同样的原因,我可以更改我想要的任何字段,然后调用保存,它也应该有效。 (这并不理想,但它解决了我的问题)

related_events = Event.objects.filter(venue=self)
        for event in related_events:  # Trigger an update on related events. This will bump the modified timestamp
            # Modify the event here
            event.save()

关于python - “UpdateQuery”对象没有属性 'get_field' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54706349/

相关文章:

python - OpenCV imwrite()/imread() 这两个函数是否以任何方式修改图像?

python - 狮身人面像 : Putting a code segment at the first line of an unordered list item

django - `SyntaxError: no binding for nonlocal ' topics_with_log_tag ' found` 虽然它是有界的

javascript - 如何在表单中手动注入(inject)NullBooleanField数据?

python - 如何从django celery任务中获取数据

python - DateField 不将 date.today 作为默认值

python - 可以在python中使用mediapipe进行人脸识别

python - 如何使用开放的 AI Clip 模型计算给定 2 个图像的图像相似度 - 哪种方法/AI 模型最适合计算图像相似度?

c++ - c++程序如何设置python代码段可以运行的环境?

python - Django读取JSON文件