推荐学习书目
› 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 · 2672 views
    This topic created in 4189 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   ·   Privacy   ·   Solana   ·   2370 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 01:49 · PVG 09:49 · LAX 18:49 · JFK 21:49
    ♥ Do have faith in what you're doing.