def _clean_data(self, data):
if isinstance(data, bytes):<EOL><INDENT>data = data.decode(errors='<STR_LIT:replace>')<EOL><DEDENT>if isinstance(data, list):<EOL><INDENT>return [self._clean_data(d) for d in data]<EOL><DEDENT>if isinstance(data, dict):<EOL><INDENT>SENSITIVE_FIELDS = {'<STR_LIT>', '<STR_LIT>', '<STR_LIT:key>', '<STR_LIT>', '<STR_LIT:password>', '<STR_LIT>'}<EOL>data = dict(data)<EOL>if self.sensitive_fields:<EOL><INDENT>SENSITIVE_FIELDS = SENSITIVE_FIELDS | {field.lower() for field in self.sensitive_fields}<EOL><DEDENT>for key, value in data.items():<EOL><INDENT>try:<EOL><INDENT>value = ast.literal_eval(value)<EOL><DEDENT>except (ValueError, SyntaxError):<EOL><INDENT>pass<EOL><DEDENT>if isinstance(value, list) or isinstance(value, dict):<EOL><INDENT>data[key] = self._clean_data(value)<EOL><DEDENT>if key.lower() in SENSITIVE_FIELDS:<EOL><INDENT>data[key] = self.CLEANED_SUBSTITUTE<EOL><DEDENT><DEDENT><DEDENT>return data<EOL>
Clean a dictionary of data of potentially sensitive info before
sending to the database.
Function based on the "_clean_credentials" function of django
(https://github.com/django/django/blob/stable/1.11.x/django/contrib/auth/__init__.py#L50)

Fields defined by django are by default cleaned with this function

You can define your own sensitive fields in your view by defining a set
eg: sensitive_fields = {'field1', 'field2'}