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

Django 和 Python 关于变量名末尾的下划线的冲突

  •  
  •   eloah · 2017-10-10 14:53:57 +08:00 · 2108 次点击
    这是一个创建于 2380 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 Python 中,有些变量名已经存在内置的方法了,比如id, type等. Python 推荐的做法是在后面加一个下划线,比如id_, type_这样来避免覆盖内置方法.然而 Django 的 ORM 中会强制检查,禁止字段名末尾使用下划线......

    这样的话,应该如何解决好一点呢......

    3 条回复    2017-10-10 15:08:01 +08:00
    privil
        1
    privil  
       2017-10-10 14:59:06 +08:00
    后面再加一个字母……
    awanabe
        2
    awanabe  
       2017-10-10 15:00:09 +08:00
    xx_id
    xx_type
    eloah
        3
    eloah  
    OP
       2017-10-10 15:08:01 +08:00
    @awanabe
    然后这样在 orm 中操作就变成了 xxx.xxx_id, xxx.xxx_type 这样了......
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1372 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:47 · PVG 07:47 · LAX 16:47 · JFK 19:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.