跨越关系的 Django 更新
Django updates that span relationships
我想要 运行 一个跨越多个关系的更新查询。这可能与 django 吗?这是我试过的:
from django.db.models import F
Transaction.objects.filter(pk__lt=10).update(
page__total_earned=F('page__total_earned')+5,
page__profile__balance=F('page__profile__balance')+5
)
下面是我的模型的一瞥,以防您想知道:
class Transaction(models.Model):
page = models.ForeignKey(Page, related_name='transactions', null=True, blank=True)
class Page(models.Model):
profile = models.ForeignKey('Profile', related_name='pages', blank=True, null=True)
total_earned = models.IntegerField(default=0)
class Profile(models.Model):
balance = models.IntegerField(default=0, db_index=True)
来自 Django docs。
Entry.objects.update(blog__name='foo') # Won't work!
The update() method is applied instantly, and the only restriction on
the QuerySet that is updated is that it can only update columns in the
model’s main table, not on related models
所以,答案是否定的,你不能那样做。
我想要 运行 一个跨越多个关系的更新查询。这可能与 django 吗?这是我试过的:
from django.db.models import F
Transaction.objects.filter(pk__lt=10).update(
page__total_earned=F('page__total_earned')+5,
page__profile__balance=F('page__profile__balance')+5
)
下面是我的模型的一瞥,以防您想知道:
class Transaction(models.Model):
page = models.ForeignKey(Page, related_name='transactions', null=True, blank=True)
class Page(models.Model):
profile = models.ForeignKey('Profile', related_name='pages', blank=True, null=True)
total_earned = models.IntegerField(default=0)
class Profile(models.Model):
balance = models.IntegerField(default=0, db_index=True)
来自 Django docs。
Entry.objects.update(blog__name='foo') # Won't work!
The update() method is applied instantly, and the only restriction on the QuerySet that is updated is that it can only update columns in the model’s main table, not on related models
所以,答案是否定的,你不能那样做。