Tsuna
Tsuna

Reputation: 2198

what's the difference between cleaned_data and normal data? - django

as mentioned in title...what's the difference? I have seen people keep on saying use cleaned_data and in videos people still say so but only says it cleans the data but when I use print

    print form.cleaned_data.get('title')
    print request.POST.get('title')

I don't see any difference though. At first I thought it's those character escape and so but tested and doesn't seem so.

which is actually a preferred way since there doesn't seem to have differences?

Upvotes: 7

Views: 6295

Answers (1)

Darshit
Darshit

Reputation: 430

You can not trust on request data as it is raw data which are not validated. You should never use data from request and store it to your database.

It so important that you store only valid data in database for smooth functioning of application. Django forms cleaned_data ensure you that validation rules defines for that form are satisfied or not. form.is_valid() check all your validations against request.POST data and generate dictionary containing valid data which is form.cleaned_data. So data retrieved from forms.cleaned_data attribute are go through form validation. It's not only validation but also converted data to relevant python type too.

You can also refer this link to know how Django form perform validation.

Upvotes: 6

Related Questions