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
python30
V2EX  ›  Python

python3 + django 前台显示 模型名 + object ? 不显示中文

  •  
  •   python30 · 2019-04-13 20:01:28 +08:00 · 1549 次点击
    这是一个创建于 1811 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这是前台发布信息的时候显示的 栏目名字



    后台显示如下:



    为什么在 python2.7+django1.11 里就前后台显示一样。都是中文

    现在刚换了 python3 前台栏目分类成了

    模型名 object 了 (Node Group object)?
    4 条回复    2019-04-14 09:43:36 +08:00
    piqizhu
        1
    piqizhu  
       2019-04-13 20:23:59 +08:00
    给模型增加一个 __str__ 方法, 你是 Python2.7 好像要换成 __unicode__

    ```python
    class Choice(models.Model):
    # ...
    def __unicode__(self):
    return self.name
    def __str__(self):
    return self.name
    ```
    piqizhu
        2
    piqizhu  
       2019-04-13 20:24:22 +08:00
    kunsunrise
        3
    kunsunrise  
       2019-04-14 03:55:53 +08:00 via iPhone
    楼上说的对,需要添加一个方法,返回一个名字,不然会按默认的显示。
    haoji
        4
    haoji  
       2019-04-14 09:43:36 +08:00 via iPhone
    一楼正解 __str__
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1385 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 23:43 · PVG 07:43 · LAX 16:43 · JFK 19:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.