Django auth.User in Admininterface: coercing to Unicode: need string or buffer, User found

梦想与她 提交于 2019-12-19 03:07:16

问题


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

My model:

from django.contrib.auth.models import User

(...)

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

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

Exception Type: TypeError

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

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

What am i missing?


回答1:


this should work and explain itself

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


来源:https://stackoverflow.com/questions/7399283/django-auth-user-in-admininterface-coercing-to-unicode-need-string-or-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!