推荐学习书目
› 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 · Apr 6, 2014 via Android · 8623 views
    This topic created in 4579 days ago, the information mentioned may be changed or developed.
    最近在学习Django,自己在看官方文档写个forum程序

    对CharField中的max_length很不解

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

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

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

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

    问题是:

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

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

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

    谢谢。
    3 replies  •  1970-01-01 08:00:00 +08:00
    RIcter
        1
    RIcter  
       Apr 6, 2014   ❤️ 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  
       Apr 6, 2014
    max_length转换成MYSQL是varchar,这个是按字符数计算的,你希望的效果只能自己写filter了
    lichifeng
        3
    lichifeng  
    OP
       Apr 7, 2014 via Android
    @RIcter 很有帮助 感谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5303 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 09:21 · PVG 17:21 · LAX 02:21 · JFK 05:21
    ♥ Do have faith in what you're doing.