Tuesday 15 May 2012

python - django Model.objects.raw returns SUM field as Decimal -


I have a dynamic model that is defined as follows:

  class Foo (models.Model): Bar = models.IntegerField (null = True) baz = models.ForeignKey (Baz) Square Baz (models.Model): bat = models.IntegerField (null = true)   

I have a Foo.objects.raw () aggregation query as follows:

  sql = "" id = "SELECT -1" id, sum (foo.bar) Join ARAP from myapp_foo on left myapp_baz Foo.baz_id = baz.id by group myapp_baz.id; "" "composite_fu = Foo.objects.raw (sql)   

All work fine Is it, but when Not use aggregate_foo.bar therefore provides a decimal rather than an int! Of course, I can put a bar in the whole number after this fact, but I want to do it correctly.

Has anyone else been run in this inappropriate "feature" of the Django RAW () function? Is SQL a proper way to write so that it returns the SAM () field as an intern? I understand that I do not need to do a raw query in the example above, but let me assume that I can not use Foo.objects.aggregate ()

for this question. This is important for this question but my datastore is MySQL 5.5

This is a feature of MySQL, and No django Therefore, the Django was not documented.

to

For numerical arguments, variation and standard deviation functions return double values. SUM () and AVG () functions return decimal values ​​for exact-value arguments (integer or DCIM), and a double value for the estimated-value arguments (float or double). (Before MySQL 5.0.3, SUM () and AVG () return again for all the numerical arguments.)

hence the result

No comments:

Post a Comment