【问题标题】:Django aggregation escapes unicode strings?Django 聚合转义 unicode 字符串?
【发布时间】:2015-03-10 10:53:33
【问题描述】:

我尝试在具有 unicode 文本的数据库上使用聚合,它显示了 unicode 对象,其中 unicode 字符再次编码。聚合后如何显示 unicode 文本?

>>> 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'}

【问题讨论】:

    标签: python django unicode django-aggregation


    【解决方案1】:

    这里没有什么特定于聚合的。在第一行中,您调用print,但在第二行中,您只是让shell 输出聚合调用的结果,它始终使用repr 格式。这会起作用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-09
      • 2021-06-30
      • 2012-03-03
      • 2015-05-27
      • 1970-01-01
      相关资源
      最近更新 更多