Thursday, October 28, 2010

How will you query?

Hi, all.

I want to increase some integer value until limit.

And.. all instance would have different increasing value and different
limit.

I modeled like this:

class Foo( models.Model ):
value = models.IntegerField(default=0)
delta = models.IntegerField(default=1)
limit = models.IntegerField(default=100)

#1: with raw SQL query
cursor.execute(
"UPDATE foo SET value = CASE WHEN value + delta < limit THEN value
+ delta ELSE limit END"
)

#2: with django's ORM (is there more elegant way?)

@transaction.commit_on_success()
def update_foo():
foo.objects.all().update( value= F("value") + F("delta") )
foo.objects.filter(value__gt=F("limit")).update( value=
F("limit") )

#1 is faster than #2 in my case and environment.

I want to know which is better Django's design. Or some another good
way?

How do you code it? :)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment