Can Django support an update by setting a field to the least of two args? I have a model that looks like:
class Bar maxval = models.FloatField() rate = models.FloatField() class Foo(models.Model): bar = models.ForeignKey(Bar) current = models.FloatField() and i'd like to increase current up to max by rate for every case where it's not yet the max... IE: I'd like to do something like: Foo.objects.filter(current__lt=F('bar__maxval')).update(current=least(Q('current')+Q('bar__rate'),Q('bar__maxval'))) But that fails because you can't add 2 query sets together, can't join field referenecs in an update, and I don't see support for 'least' What I've done instead is rewritten it to iterate over bars each time filtering on foo like: set = foo.objects.filter(bar=bar).filter(current__lt=bar.max) and then compute the min amount of current to hit max this update, and update that, then iterate over what remains and update each one individually, ie: canreachfull = bar.max - bar.rate set.filter(current__gte=canreachfull).update(current=bar.rate) set = foo.objects.filter(bar=bar).filter(current__lt=bar.max) for record in set: newcurrent = record.current + bar.max record.update(current=newcurrent) Is there a better way to do this? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.