I would like to run an update query that spans several realtionships. Is this possible with django? This is what I have tried:
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
)
Here is a glimpse of my models in case you are wondering:
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)
From 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
So, the answer is not, you can't do it in that way.