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

问教一个 Django 的 max_length 问题

  •  
  •   lichifeng · 2014-04-06 21:04:56 +08:00 via Android · 7843 次点击
    这是一个创建于 3665 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近在学习Django,自己在看官方文档写个forum程序

    对CharField中的max_length很不解

    Django中的max_length似乎是指字符的个数?

    比如我设定max_length=4,那我在admin后台中最多可以输入4个英文字符
    也可以输入4个中文字符

    别外Django模板中的truncatechars过滤器也是这样

    像{{ value|truncatechars:9 }}就会限制在9个英文字符,中文也是9个

    问题是:

    我希望设定的是最大的字节(?)不是字符的个数

    例如最大可以是4个中文或8个英文字符这种

    不知道问题出在哪里?应该如何着手?

    谢谢。
    3 条回复    1970-01-01 08:00:00 +08:00
    RIcter
        1
    RIcter  
       2014-04-06 21:07:55 +08:00   ❤️ 1
    Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win
    32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a=u'啊'
    >>> len(a)
    1
    >>> a='啊'
    >>> len(a)
    2
    >>>


    不谢= =!
    mengzhuo
        2
    mengzhuo  
       2014-04-06 22:44:55 +08:00
    max_length转换成MYSQL是varchar,这个是按字符数计算的,你希望的效果只能自己写filter了
    lichifeng
        3
    lichifeng  
    OP
       2014-04-07 07:54:01 +08:00 via Android
    @RIcter 很有帮助 感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5343 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:05 · PVG 17:05 · LAX 02:05 · JFK 05:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.