Rya*_*axe 26 python sql django
所以我试图通过运行以下代码来更新我的模型:
FooBar.objects.filter(something=True).update(foobar=F('foo__bar'))
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
FieldError: Joined field references are not permitted in this query
Run Code Online (Sandbox Code Playgroud)
如果F
表达式不允许这样做...我怎样才能实现此更新?
鉴于这张票中的信息,我现在明白这是不可能的,永远不会在django中实现,但有没有办法实现这个更新?也许有一些解决方法?我不想使用循环,因为有超过1000万个FooBar
对象,所以SQL比python快得多.
Geo*_*hev 12
Django 1.11添加了对子查询的支持。您应该能够:
from django.db.models import Subquery, OuterRef
FooBar.objects.filter(something=True).update(
foobar=Subquery(FooBar.objects.filter(pk=OuterRef('pk')).values('foo__bar')[:1])
)
Run Code Online (Sandbox Code Playgroud)
这是 Georgi Yanchev 对两个模型的回答的实现:
class Foo(models.Model):
bar = models.ForeignKey(Bar)
Foo.objects \
.filter(foo_field_1=True) \
.update(foo_field_2=Subquery(
Bar.objects \
.filter(id=OuterRef('bar_id')) \
.values('bar_field_1')[:1]))
Run Code Online (Sandbox Code Playgroud)
为什么不在这里使用原始的sql:基于此,它将是类似的东西
from django.db import connection
raw_query = '''
update app_foobar set app_foobar.foobar =
(select app_foo.bar from app_foo where app_foo.id = app_foobar.foo_id)
where app_foobar.something = 1;
'''
from django.db import connection
cursor = connection.cursor()
cursor.execute(raw_query)
Run Code Online (Sandbox Code Playgroud)