V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
onefouroo
V2EX  ›  Python

请教大家一个 django 的问题

  •  
  •   onefouroo · 2016-09-20 18:34:15 +08:00 · 2598 次点击
    这是一个创建于 2767 天前的主题,其中的信息可能已经有所发展或是发生改变。
    model 如下
    class Student(models.Model):
    user = models.ForeignKey('auth.User', null=True, blank=True, verbose_name=u'学员')
    created = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    def __unicode__(self):
    return self.user

    然后运行
    然后报错
    coercing to Unicode: need string or buffer, User found

    然后修改
    def __unicode__(self):
    return str(self.user)

    然后就不报错了。
    我 google 半天也不知道为什么,求大神指点一下
    15 条回复    2016-09-25 16:20:00 +08:00
    onefouroo
        1
    onefouroo  
    OP
       2016-09-20 18:36:32 +08:00
    第一次发帖,不知道怎么缩进
    xidianlz
        2
    xidianlz  
       2016-09-20 18:39:35 +08:00
    看报错信息, self.user 不是 str 的
    onefouroo
        3
    onefouroo  
    OP
       2016-09-20 18:43:27 +08:00
    @xidianlz 你好
    但是我把 model 改了一下
    class Student(models.Model):
    user = models.ForeignKey('auth.User', null=True, blank=True, verbose_name=u'学员')
    user_fullname = models.CharField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    def __unicode__(self):
    return self.user_fullname

    报同样的错误, user_fullname 我输入的肯定是 str 但是报同样错误
    Shawdon
        4
    Shawdon  
       2016-09-20 18:51:05 +08:00   ❤️ 1
    LZ 先 google 下这个函数__unicode__,了解下它的相关信息。
    你的 user_fullname 也不是一个 str ,而是 models.CharField **对象**!!
    kkx
        5
    kkx  
       2016-09-20 18:55:59 +08:00 via iPhone
    @onefouroo return u"{}".format(self.user)
    saturnisbig
        6
    saturnisbig  
       2016-09-20 19:00:59 +08:00 via Android
    self.user 是个 foreignkey
    onefouroo
        7
    onefouroo  
    OP
       2016-09-20 19:41:54 +08:00
    @Shawdon
    @kkx
    @saturnisbig

    谢谢大家。突然明白了。
    glasslion
        8
    glasslion  
       2016-09-20 22:28:52 +08:00
    @onefouroo 你确定 user_fullname 会报一样的错?
    glasslion
        9
    glasslion  
       2016-09-20 22:30:26 +08:00
    @Shawdon CharField 这个 descrispter 在__get__ 访问时返回的 unicode
    georgema1982
        10
    georgema1982  
       2016-09-21 04:28:09 +08:00
    错误原因你应该已经明白了。但是我不赞同你用 str(self.user) 。最好是用 django 的 force_text
    Shawdon
        11
    Shawdon  
       2016-09-21 10:53:32 +08:00
    @glasslion 何以见得?
    glasslion
        12
    glasslion  
       2016-09-21 11:38:57 +08:00
    @Shawdon 你没用过 django?
    Shawdon
        13
    Shawdon  
       2016-09-22 10:30:14 +08:00
    @glasslion 用过。我的意思是 “ return self.user_fullname ” 怎么会返回 unicode 。。
    glasslion
        14
    glasslion  
       2016-09-22 12:44:16 +08:00   ❤️ 1
    @Shawdon CharField 在访问时的返回的就是 unicode

    ```
    class CharField(Field):

    def to_python(self, value):
    if isinstance(value, six.string_types) or value is None:
    return value
    return force_text(value)
    ```

    原理: https://code.djangoproject.com/wiki/DevModelCreation
    Shawdon
        15
    Shawdon  
       2016-09-25 16:20:00 +08:00
    @glasslion 今天才有时间好好读读链接里的内容,受益匪浅。兄弟对 Django 或 Python 的造诣是远远大于我的(手动赞),谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   950 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:16 · PVG 05:16 · LAX 14:16 · JFK 17:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.