开发者

How to work with ModelForm and ModelFormSet

开发者 https://www.devze.com 2023-03-31 17:28 出处:网络
I just want some clarification. I\'m working through the Django Form Wizard documentation In the documentation it talks about the Form Wizard being able to \'work\' with ModelForm and ModelFormSet. I

I just want some clarification. I'm working through the Django Form Wizard documentation

In the documentation it talks about the Form Wizard being able to 'work' with ModelForm and ModelFormSet. I want 开发者_运维知识库clarification on what this means. Does it mean that the Form Wizard can save the post data of a form straight to the database via the model?

I'm passing the instance of a model as an argument in the instance_dict parameter without success. When I submit the form the form gets processed but nothing is sent to the database.

Thanks


No, it is not saving the form information automatically!

Usually you need to process the form in your view. A ModelForm is basically just a helper to make model instance edting/creating easier and of course more native for your frontend. So here something you usually would do in your view (in short):

form = MyForm(data=request.POST)
if form.is_valid():   # checks of the form and its fields validate
    form.save()
# else you can do something else, show the validation errors for example

For more details you should check the docs about how to work with forms and the ModelForm documentation. Starting direclty with the FormWizard wasn't maybe such a good idea. :)

0

精彩评论

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

关注公众号