Difference between cleaned_data and cleaned_data.get in Django

后端 未结 2 383
清歌不尽
清歌不尽 2021-02-02 15:25

I\'ve seen some samples codes like:

    def clean_message(self):
    message = self.cleaned_data[\'message\']
    num_words = len(message.split())
    if num_wor         


        
2条回答
  •  情歌与酒
    2021-02-02 16:00

    cleaned_data is 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’)
    

    Difference between cleaned_data and cleaned_data.get in Django is that if the key does not exist in the dictionary, self.cleaned_data[‘field’] will raise a KeyError, while self.cleaned_data.get(‘field’) will return None.

提交回复
热议问题