What are the differences between these two ways to save form information?

Asked

Viewed 105 times

0

I am learning how to use Jango forms and I have been presented with two ways to save information from a form in a database. I tested both and both work with validations, but did not understand what the difference between one and the other.

The former uses functions inherited from the models class. Model, using the function "Objects.create(**form.cleaned_data)" and passing the form fields as parameter.

In the example below, I introduce the Productform class that inherits from 'Forms.Modelform' and use the function 'cleaned_data' to pass as parameter.

def product_create_view(request):

    form = ProductForm()

    if request.method == 'POST':
        form = ProductForm(request.POST or None)

        if form.is_valid():
            Product.objects.create(**form.cleaned_data)
            form = ProductForm()

    data = {}
    data['form'] = form

    return render(request, 'products/product_create.html', data)

The second way instead of using the function 'Objects.create(**Dict)' uses the form.save() directly.

def product_create_view(request):

    form = ProductForm()

    if request.method == 'POST':
        form = ProductForm(request.POST or None)

        if form.is_valid():
            form.save()
            form = ProductForm()

    data = {}
    data['form'] = form

    return render(request, 'products/product_create.html', data)

Briefly, I would like to know there are differences between using form.save() and using Product.objects.create(**Dict).

1 answer

0

I got an answer on stackoverflow in English that cleared the doubt. The translation I made was like this.

You can only use the form.save() when the form class inherits from ModelForm. Overall this is the easiest way to create and update data from a Model. Note that in these cases form.save() returns a saved instance of the object, so you can write product = form.save() and do other operations with the object saved.

If you create a form that only inherits from the class Form, this form will have no link with any specific model and soon it will not be possible to save with form.save(). This way you will have to create an object using as parameters the form data with the function cleaned_data. You will have to define all form fields with the same names as the Model, which is a lot more work than using the ModelForm, where you can only list the Model parameters.

In the end there is no difference in the results, but it is generally preferable to use form.save() inheriting from Modelform, as it is more readable and simpler to understand what is being done.

  • 1

    Your answer is not wrong but it is not enough, there are many situations in which form.save() not suitable, examples: Imagine that according to the user’s choice, before persisting the data you have to perform a calculation or consult an api to update a field not present in the form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.