【问题标题】:Unicode question in Python with Django使用 Django 的 Python 中的 Unicode 问题
【发布时间】:2010-12-06 20:40:48
【问题描述】:

我在模型中有一个函数client_groups,它返回“组”对象列表:

return self.userprofile.client.get_groups()

例如。返回列表的:

[<Group: Finance>, <Group: Recepção>,...]

如果我只是在 shell 中正常打印这些内容,例如 print groups[1],它会显示正确的内容,即 "Recepção"。 这个函数在我的 admin.py 中使用,在我的 list_display 元组中。 我该如何去显示

Finance, Recepção,...

在我的 Django 管理界面中? 我尝试对其进行解码,但出现 UnicodeEncodeError: 'ascii' codec can't encode characters。

在我的 admin.py 中: list_display = ("username","email","first_name","last_name","number","client","client_groups","date_created",)

【问题讨论】:

    标签: django unicode


    【解决方案1】:
    u', '.join(group.someattr for group in self.userprofile.client.get_groups())
    

    【讨论】:

    • 我运行了你的线路,它成功了。出于好奇,我在 shell 中运行了我的原始代码,并将我的输出与新输出进行了比较。据我所知,没有区别。您能否解释一下为什么您的代码有效,除了您使用“unicoded”逗号空格加入组项目的事实?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2013-03-21
    • 2015-07-07
    • 2011-01-07
    • 2018-09-28
    相关资源
    最近更新 更多