What is the use of cleaned_data in Django
Asked Answered
W

2

18

Django says if form.is_valid() is True. form.cleaned_data is where all validated fields are stored. But I am confused about using the cleaned_data function.

form.cleaned_data['f1'] -- cleaned data request.POST.get('f1') -- un-validated data

I have a model form in Django.

if form1.is_valid():
    form1.save()

Does this save cleaned_data to the model or does it save unvalidated data.

form2=form1.save(commit=False);

Does form2 contain form1's cleaned_data or unvalidated data.

Apart from converting any date to python datetime object, is there a good example of benefit of using cleaned_data vs unvalidated data. Thanks

Warnock answered 3/12, 2018 at 13:20 Comment(2)
Why would it save uncleaned data? Anyway, if the form is valid, there isn't any uncleaned data.Jolynnjon
So form1.cleaned_data[' '] should only be used when checking individual form field? And if form.is_valid() ==True, then there is no need to access individual field cleaned_data. form1['field1'] == form1.cleaned_data['field1'] ?Warnock
U
27

There 2 situations: using basic Form (forms.Form) and ModelForm (forms.ModelForm).

If you are using a ModelForm then there is no any need of playing with a cleaned_data dictionary because when you do form.save() it is already be matched and the clean data is saved. But you are using basic Form then you have to manually match each cleaned_data to its database place and then save the instance to the database not the form.

For example basic Form:

if form.is_valid():
    ex = Example()
    ex.username = form.cleaned_data['username']
    ex.save()

For example ModelForm:

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

NOTE: If the form pass from is_valid() stage then there is no any unvalidated data.

Underpinning answered 3/12, 2018 at 15:16 Comment(2)
form1.save() or form2=form1.save(commit=False) wont work if form is invalid. Which also means that these functions only save valid inputs to DB. Use python manage.py shell to try it outWarnock
Yes that is right. That is why they are put in is_valid() clause. Django tries to make everything error free and without it you should yourself had to validate the input data.Underpinning
G
43

TL;DR

form.cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects.

form.data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).

Example by code

In my forms.py I have two fields:

class Loginform(forms.Form):
    username=forms.CharField()
    password=forms.CharField(widget=forms.PasswordInput)

and in my views.py:

def login_page(request):
    form=Loginform(request.POST or None)
    if form.is_valid():
        print(form.cleaned_data)

The above code prints the following output:

{'username': 'xyz', 'password': 'shinchan'}

If instead views.py contains:

def login_page(request):
    form=Loginform(request.POST or None)
    if form.is_valid():
        print(form)

The above code prints the following:

 <tr><th><label for="id_username">Username:</label></th><td><input type="text" name="username" value="xyz" required id="id_username"></td></tr>
<tr><th><label for="id_password">Password:</label></th><td><input type="password" name="password" required id="id_password"></td></tr>
Gavel answered 12/3, 2020 at 3:28 Comment(0)
U
27

There 2 situations: using basic Form (forms.Form) and ModelForm (forms.ModelForm).

If you are using a ModelForm then there is no any need of playing with a cleaned_data dictionary because when you do form.save() it is already be matched and the clean data is saved. But you are using basic Form then you have to manually match each cleaned_data to its database place and then save the instance to the database not the form.

For example basic Form:

if form.is_valid():
    ex = Example()
    ex.username = form.cleaned_data['username']
    ex.save()

For example ModelForm:

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

NOTE: If the form pass from is_valid() stage then there is no any unvalidated data.

Underpinning answered 3/12, 2018 at 15:16 Comment(2)
form1.save() or form2=form1.save(commit=False) wont work if form is invalid. Which also means that these functions only save valid inputs to DB. Use python manage.py shell to try it outWarnock
Yes that is right. That is why they are put in is_valid() clause. Django tries to make everything error free and without it you should yourself had to validate the input data.Underpinning

© 2022 - 2024 — McMap. All rights reserved.