Search code examples
pythondjangounicodedjango-aggregation

Django aggregation escapes unicode strings?


I tried to use aggregation on a database with unicode texts and it showed unicode object with unicode characters encoded another time. What can I do to show the unicode text after aggregation?

>>> from apps.person.models import Person
>>> from django.db.models import Min
>>> for p in Person.objects.all()[:1]: print(p.full_name)
...
15 чоловік
>>> Person.objects.aggregate(Min('full_name'))
{'full_name__min': u'15 \u0447\u043e\u043b\u043e\u0432\u0456\u043a'}

Solution

  • There's nothing specific to aggregation here. In the first line, you are calling print, but in the second you are just letting the shell output the result of the aggregate call, which always uses the repr format. This would work:

    result = Person.objects.aggregate(Min('full_name'))
    print result['full_name__min']