管理界面中的 Django auth.User:强制转换为 Unicode:需要字符串或缓冲区,找到用户 [英] Django auth.User in Admininterface: coercing to Unicode: need string or buffer, User found

查看:20
本文介绍了管理界面中的 Django auth.User:强制转换为 Unicode:需要字符串或缓冲区,找到用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 django 还是很陌生.我尝试使用 auth.User 对象作为外键.

I'm pretty new to django. I try to use the auth.User object as a foreign key.

我的模特:

from django.contrib.auth.models import User

(...)

class Entry(models.Model):
    (...)
    user = models.ForeignKey(User)
    date = models.DateTimeField()
    def __unicode__(self):
        return self.user

在管理界面中与用户创建新条目时,我得到:强制转换为 Unicode:需要字符串或缓冲区,找到用户"

When creating a new Entry with a user in admin interface, i get: "coercing to Unicode: need string or buffer, User found"

异常类型:TypeError

Exception Type: TypeError

异常值:强制转换为 Unicode:需要字符串或缓冲区,用户找到了

Exception Value: coercing to Unicode: need string or buffer, User found

例外位置:/Library/Python/2.7/site-packages/django/utils/encoding.py在 force_unicode 中,第 71 行

Exception Location: /Library/Python/2.7/site-packages/django/utils/encoding.py in force_unicode, line 71

我错过了什么?

推荐答案

这应该可以工作并解释自己

this should work and explain itself

def __unicode__(self):
    return unicode(self.user)

这篇关于管理界面中的 Django auth.User:强制转换为 Unicode:需要字符串或缓冲区,找到用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆