10👍
I’m not running the same Django version, but I think this’ll work:
MyModel.objects.all().aggregate(latest=Max(Func(F('created_ts'), function='UNIX_TIMESTAMP')))
Note the latest
keyword argument in there. That’s the key (I think, again I can’t test this).
7👍
aggregate() is a terminal clause for a QuerySet that, when invoked,
returns a dictionary of name-value pairs
It will automatically provide this value in many generic cases, e.g.
Sum('items') -> sum_items
For your query, it cannot create a default alias, so you must provide one. This is so the query can return a named result for the values produced. All you have to do is give your aggregate a meaningful named alias and all should work fine:
MyModel.objects.all().aggregate(max_created=Max(Func(F('created_ts'), function='UNIX_TIMESTAMP')))
- How to get Interdependent dropdowns in django using Modelform and jquery?
- Django Inline Formsets using custom form
- Celery task and customize decorator
0👍
just need set name for result variable :
see item
in below code
result = MyModel.objects.all().aggregate(item=Max(Func(F('created_ts'), function='UNIX_TIMESTAMP')))
result.get('item')
- How to use custom managers in chain queries?
- Django 'function' object has no attribute 'objects'
- Django: Form field size
- Django: Possible to load fixtures with date fields based on the current date?
Source:stackexchange.com