Django update queryset with annotation

前端 未结 6 598
梦谈多话
梦谈多话 2020-12-24 01:06

I want to update all rows in queryset by using annotated value.

I have a simple models:

class Relation(models.Model):
    rating = models.IntegerFiel         


        
6条回答
  •  情书的邮戳
    2020-12-24 01:44

    You really can't do this. Take a look at the code for update and follow it through for some fine reading.

    Honestly, what's wrong with placing something like this in a Manager definition? Put those 3 lines you don't want to put in your view into a manager, call that manager as necessary. Additionally, you're doing much less "magic" and when the next developer looks at your code, they won't have to resort to a few WTF's .. :)

    Also, I was curious and it looks like you can use SQL Join with UPDATE statements but it's some classic SQL hackery .. So if you're so inclined, you can use Djangos raw SQL functionality for that ;)

提交回复
热议问题