0

I want to update model fields and fields of related model in one query in Django:

Link.objects.filter(alpha=True).update(
    alpha=False,
    target__backup_of_original_start=F('target__original_start'),
    target__backup_of_original_end=F('target__original_end'),
    target__original_start=F('target__start'),
    target__original_end=F('target__end'),
)

In this question I see that it is impossible using update. Is it possible to overcome this restriction using Django ORM or at least plain SQL? And how?

shpindler
  • 357
  • 2
  • 12
  • 1
    Is it mandatory to be in just one update? – Brian Destura Aug 16 '21 at 08:23
  • @bdbd any suggestions to minimize db queries are welcome – shpindler Aug 16 '21 at 08:54
  • In Django ORM, everything you do is centered on only one model, avoiding kraken-style work. I suggest to stick to that style (which is nice for understandability) as long as you can. Only search for alternatives if your query really turns out to be a bottleneck. – Lutz Prechelt Aug 16 '21 at 09:17

1 Answers1

-1

You can create SQL Stored Procedures and SQL Views to make only one DB request.

Helpful resource: What is the best way to access stored procedures in Django's ORM

Tiino
  • 78
  • 9