Just to be sure though, I don't need to call save on the instances unless I need to emit the save events right? Reading the manual: Be aware that the update() method is converted directly to an SQL statement. It is a bulk operation for direct updates. It doesn't run any save() methods on your models, or emit the pre_save or post_save signals (which are a consequence of calling save()). If you want to save every item in a QuerySet and make sure that the save() method is called on each instance, you don't need any special function to handle that. Just loop over them and call save():
It implies that it's running the UPDATE SQL command directly so the data is getting changed regardless of if I invoke save() myself. Correct? On Sun, Jul 11, 2010 at 2:03 AM, euan.godd...@googlemail.com <euan.godd...@gmail.com> wrote: > Django doesn't support the query type you're trying to do. You've > already discovered F which is about as complicated as it gets, bt the > update syntax is pretty much limited to setting a field's value to a > consant. > > Looping over the minimum set of things and calling save on each one if > your only option without resorting to custom SQL. > > Euan > > On 11 July, 07:44, Doug Warren <doug.war...@gmail.com> wrote: >> 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. > > -- 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.