Why does Django Queryset say: TypeError: Complex aggregates require an alias? -
i have django class follows:
class mymodel(models.model): my_int = models.integerfield(null=true, blank=true,) created_ts = models.datetimefield(default=datetime.utcnow, editable=false)
when run following queryset, error:
>>> django.db.models import max, f, func >>> mymodel.objects.all().aggregate(max(func(f('created_ts'), function='unix_timestamp'))) traceback (most recent call last): file "<console>", line 1, in <module> file "myvirtualenv/lib/python2.7/site-packages/django/db/models/query.py", line 297, in aggregate raise typeerror("complex aggregates require alias") typeerror: complex aggregates require alias
how adjust queryset don't error? want find instance of mymodel latest created_ts. , want use unix_timestamp
function it.
i'm not running same django version, think this'll work:
mymodel.objects.all().aggregate(latest=max(func(f('created_ts'), function='unix_timestamp')))
note latest
keyword argument in there. that's key (i think, again can't test this).
Comments
Post a Comment