V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
pythonee
V2EX  ›  Python

[django]如何校验多个字段呢

  •  
  •   pythonee · Apr 13, 2015 · 2482 views
    This topic created in 4034 days ago, the information mentioned may be changed or developed.
    model每个字段有单独的校验器
    可是如果我想校验一个model的多个字段怎么办呢

    我能想到的就是处理pre_save信号,但是这样不通过只能抛出异常,友好性欠佳
    2 replies    2015-04-14 09:45:44 +08:00
    pythoner
        1
    pythoner  
       Apr 13, 2015
    models只跟增删改查打交道,不要把太多无关的操作塞进去,以后拆分起来会很痛苦的
    数据校验用forms来作,除了默认的类型验证之外,可以在自定义的clean_xxx 方法中抛出ValidationError的异常
    pythonee
        2
    pythonee  
    OP
       Apr 14, 2015
    @pythoner 主要是校验多个字段,昨天试了下model的clean方法,应该是可以实现的,这里是admin添加的校验啦
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1391 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 23:58 · PVG 07:58 · LAX 16:58 · JFK 19:58
    ♥ Do have faith in what you're doing.