#28762: Can't aggregate annotations with array parameters
-------------------------------------+-------------------------------------
Reporter: Daniel | Owner: nobody
Keller |
Type: | Status: new
Uncategorized |
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
If I use a database function that takes an array parameter, for example a
histogram:
{{{
import math
from django.db import models
from django.contrib.postgres.fields import ArrayField
class Log2Bucket(models.Func):
function = 'width_bucket'
def __init__(self, expression, a, b, **extra):
max_exp = math.ceil(math.log2(b / a))
buckets = [a * 2**exp for exp in range(max_exp + 1)]
buckets = models.Value(buckets,
output_field=ArrayField(models.FloatField())) ###
super().__init__(expression, buckets,
output_field=models.PositiveSmallIntegerField(), **extra)
}}}
and a simple model
{{{
class Foo(models.Model):
bar = models.FloatField()
}}}
It works fine in queries like
{{{
Foo.objects.annotate(b=Log2Bucket('bar', 1, 100)).values('b')
}}}
but for
{{{
Foo.objects.annotate(b=Log2Bucket('bar', 1,
100)).values('b').annotate(count=models.Count('pk'))
}}}
I get
{{{
...
File "/home/daniel_keller/shared/raumplus_2/env/lib/python3.4/site-
packages/django/db/models/sql/compiler.py", line 128, in get_group_by
if (sql, tuple(params)) not in seen:
TypeError: unhashable type: 'list'
}}}
referring to the list passed to `Value`.
Honestly, I'm not sure if this counts as a bug or a new feature, since
it's clearly unintended behavior, but none of Django's database functions
need it to work.
I can work around it by replacing the marked line with
{{{
buckets = Value("{%s}" % ','.join(map(str, buckets))) ###
}}}
but this is IMO very ugly.
--
Ticket URL: <https://code.djangoproject.com/ticket/28762>
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/055.5658ef318697fac142db3c457767019e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.