#25643: Extend expressions API to update()
----------------------------------------------+--------------------
     Reporter:  jorgecarleitao                |      Owner:  nobody
         Type:  New feature                   |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  master
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 Given the following models:

 {{{
 class Relation(models.Model):
     rating = models.IntegerField(default=0)

 class SignRelation(models.Model):
     relation = models.ForeignKey(Relation, related_name='sign_relations')
     rating = models.IntegerField(default=0)
 }}}

 support queries like

 {{{
 Relation.objects.update(rating=Sum('sign_relations__rating'))

 
Relation.objects.annotate(total_rating=Sum('sign_relations__rating')).update(rating=F('total_rating'))
 }}}

 to avoid queries like

 {{{
 for relation in
 Relation.objects.annotate(total_rating=Sum('sign_relations__rating')):
     relation.rating = relation.total_rating or 0
     relation.save()
 }}}

 This is useful to populate models that contain redundant data.

 Based on [http://stackoverflow.com/questions/3652736/django-update-
 queryset-with-annotation this question] in SO.

--
Ticket URL: <https://code.djangoproject.com/ticket/25643>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/057.3cb93a5fc0c0c88545c535b2b5e2ebd9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to