Django update queryset with annotation

前端 未结 6 556
梦谈多话
梦谈多话 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:43

    You can define your own custom objects manager:

    class RelationManager(models.Manager):
        def annotated(self,*args,*kwargs):
             queryset = super(RelationManager,self).get_queryset()
             for obj in queryset:
                   obj.rating = ... do something ...
             return queryset
    
    class Relations(models.Model):
        rating = models.IntegerField(default=0)
        rating_objects = RelationManager()
    

    Then in your code:

    q = Realation.rating_objects.annotated()
    

    Add args/kwargs to customise what this manager returns.

    0 讨论(0)
  • 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 ;)

    0 讨论(0)
  • 2020-12-24 01:46

    If you want to avoid many calls to the database, you should use transaction.atomic.

    Read more on Django documentation: https://docs.djangoproject.com/en/1.9/topics/db/transactions/#controlling-transactions-explicitly

    0 讨论(0)
  • 2020-12-24 01:49

    For Django 1.11+ you can use Subquery:

    from django.db.models import OuterRef, Subquery, Sum
    
    Relation.objects.update(
        rating=Subquery(
            Relation.objects.filter(
                id=OuterRef('id')
            ).annotate(
                total_rating=Sum('sign_relations__rating')
            ).values('total_rating')[:1]
        )
    )
    

    This code produce the same SQL code proposed by Tomasz Jakub Rup but with no use of RawSQL expression. The Django documentation warns against the use of RawSQL due to the possibility of SQL injection).

    Update

    I published an article based on this answer with more in-depth explanations: Updating a Django queryset with annotation and subquery on paulox.net

    0 讨论(0)
  • 2020-12-24 01:50

    Workaround for postgres:

    with connection.cursor() as cursor:
        sql, params = qs.query.sql_with_params()
        cursor.execute("""
            WITH qs AS ({})
            UPDATE foo SET bar = qs.bar
            FROM qs WHERE qs.id = foo.id
        """.format(sql), params)
    
    0 讨论(0)
  • 2020-12-24 02:07

    UPDATE statement doesn't support GROUP BY. See e.g. PostgreSQL Docs, SQLite Docs.

    You need someting like this:

    UPDATE relation
    SET rating = (SELECT SUM(rating)
                  FROM sign_relation
                  WHERE relation_id = relation.id)
    

    Equivalent in DjangoORM:

    from django.db.models.expressions import RawSQL
    
    Relation.objects.all(). \
        update(rating=RawSQL('SELECT SUM(rating) FROM signrelation WHERE relation_id = relation.id', []))
    

    or:

    from django.db.models import F, Sum
    from django.db.models.expressions import RawSQL
    
    Relation.objects.all(). \
        update(rating=RawSQL(SignRelation.objects. \
                             extra(where=['relation_id = relation.id']). \
                             values('relation'). \
                             annotate(sum_rating=Sum('rating')). \
                             values('sum_rating').query, []))
    
    0 讨论(0)
提交回复
热议问题