推荐学习书目
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 · Apr 13, 2019 · 2087 views
    This topic created in 2654 days ago, the information mentioned may be changed or developed.
    这是前台发布信息的时候显示的 栏目名字



    后台显示如下:



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

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

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

    ```python
    class Choice(models.Model):
    # ...
    def __unicode__(self):
    return self.name
    def __str__(self):
    return self.name
    ```
    kunsunrise
        3
    kunsunrise  
       Apr 14, 2019 via iPhone
    楼上说的对,需要添加一个方法,返回一个名字,不然会按默认的显示。
    haoji
        4
    haoji  
       Apr 14, 2019 via iPhone
    一楼正解 __str__
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4301 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 05:28 · PVG 13:28 · LAX 22:28 · JFK 01:28
    ♥ Do have faith in what you're doing.