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

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

  •  
  •   pythonee · 2015-04-13 19:45:22 +08:00 · 2077 次点击
    这是一个创建于 3337 天前的主题,其中的信息可能已经有所发展或是发生改变。
    model每个字段有单独的校验器
    可是如果我想校验一个model的多个字段怎么办呢

    我能想到的就是处理pre_save信号,但是这样不通过只能抛出异常,友好性欠佳
    2 条回复    2015-04-14 09:45:44 +08:00
    pythoner
        1
    pythoner  
       2015-04-13 20:59:22 +08:00
    models只跟增删改查打交道,不要把太多无关的操作塞进去,以后拆分起来会很痛苦的
    数据校验用forms来作,除了默认的类型验证之外,可以在自定义的clean_xxx 方法中抛出ValidationError的异常
    pythonee
        2
    pythonee  
    OP
       2015-04-14 09:45:44 +08:00
    @pythoner 主要是校验多个字段,昨天试了下model的clean方法,应该是可以实现的,这里是admin添加的校验啦
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1075 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 19:12 · PVG 03:12 · LAX 12:12 · JFK 15:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.