clean() method in model and field validation

前端 未结 3 1896
挽巷
挽巷 2020-12-28 16:37

I have a problem with a model\'s clean() method and basic field validation. here\'s my model and the clean() method.

class Trial(mo         


        
3条回答
  •  旧巷少年郎
    2020-12-28 17:25

    I know this is late, but to answer why this might be happening for people who end up here: There's no "original clean". The clean method is a hook for custom validation, so you are the one who provides its code. Not sure how OP was using the clean hook, but: After defining it you should be calling full_clean() over your models so that Django runs model validation in its entirety. See the details in the docs for the order in which it calls the different validation methods and such https://docs.djangoproject.com/en/1.11/ref/models/instances/#validating-objects (perhaps important to note: full_clean() isn't automatically called by a model's save() which is part of why when you use the shell and save straight away you'll be skipping the validation)

    (note ModelForms also have various validation steps and will call a model's full_clean: https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#validation-on-a-modelform )

提交回复
热议问题