开发者

Django ModelForm vs. Form

Hi Everyone

I want to know if a ModelForm is better than a Form to write a form for a normal user that has complicated form validation and开发者_开发技巧 more fields than a Model. Which one is more professional?

Thanks


ModelForms are just a matter of convenience: if your form can be easily be mapped to model, use a ModelForm. Validation is basically the same for ModelForms and regular Forms, but using ModelForms will save you the hassle of writing the some of the validation rules by hand.


ModelForm is a "shortcut" that creates a form that will work with and save a model.

If there is not a single Model associated with your form use a Form instead.


Honestly, modelForm is best use because it eliminate redundancy and save you more time. The modelform is use to create a form from Django model class. Therefore, if you are sure you have no field in your Django model that is related or the same with the form, you can go ahead with using Django form.


ModelForm is when you have to either put all of the Model likewise, you can just use select column names, or either "all"

Whereas a typical Form can be merged within FormSet, and is more open towards not directly saving to model but to be processed within View logic functions and afterwards can be saved or cannot based upon the requirement

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜