Django Advantage forms.Form vs forms.ModelForm

The question is very similar to this question, but I wanted to ask it differently.

I am a very tuned guy, but sometimes I like to make shortcuts. So there it is.

I find these two classes very similar, although one “helps” the programmer write code faster or have less code / repeating code. Connecting models to forms sounds like an obvious thing. One thing that is not particularly clear in documents using ModelForm. What happens if you need to add additional fields that are not in the Model or are somehow related to another model?

I think you could subclass this and make it work, but will it really save you time than just doing it with a form?

So, the following question may not have a definite answer if I subclass it and use ModelForm. Is ModelForm especially faster than Form? Does he use the same update methods or bind much faster in one or the other?

+3
source share
1 answer

If you need a form for two models, you have a couple of options:

1) create two model templates, save them separately when publishing, and if one of them depends on the other (for example, a foreign key), set this in your view before saving.

2) try Django inline formset: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-an-inline-formset-in-a-view

3) . ModelForm , . cleaned_data, , .

, ModelForm - ( , , , ). ,

+3

Source: https://habr.com/ru/post/1767303/


All Articles