Django Forms – The difference between self.cleaned_data[‘field’], self.cleaned_data.get(‘field’) and self.data

By understanding how the attributes self.cleaned_data and self.data work and how to get its values, you can control and use them efficiently in your forms.

Defining the form

This is a simple ModelForm example for a model named Car.

I created the clean_price method to change the original format of the price field that will be passed to the form. This is particularly important, because it’s a key to understand the difference between self.cleaned_data and self.data.

Passing data to the form

The first thing to do is to create a dictionary and pass to the form as the first parameter.

What is self.data?

self.data is simply the original data passed to the form. So it will return the original data:

What is self.cleaned_data?

self.cleaned_data is an dictionary that receives all cleaned and validated data after calling the is_valid() method.

At this point, if we try to access the cleaned_data attribute, an AttributeError will be raised.

This happened because the cleaned_data attribute is created when we call the is_valid() method. After calling the is_valid() method, we have access to the cleaned_data attribute.

And, of course, we also have access to the original data passed to the form. Now, we can see the original and modified price after validation by the clean_price method:

The difference between self.cleaned_data[‘field’] and self.cleaned_data.get(‘field’)

cleaned_data is a Python dictionary and an attribute of the form object. As a Python dictionary, you can access its values by:

  • Specifying the key between [ ]: self.cleaned_data[‘field’]
  • Using get() method: self.cleaned_data.get(‘field’)

The difference between these two approaches is that if the key does not exists, self.cleaned_data[‘field’] will raise a KeyError, while self.cleaned_data.get(‘field’) will return None.

As you can see in the example below, the get() method also allows us to specify a default value if the key does not exists: