[Django]-Django REST Framework's APIClient sends None as 'None'

4👍

✅

The problem here is that the APIClient is sending data to the view as form-data by default, which doesn’t have a concept of None or null, so it is converted to the unicode string None.

The good news is that Django REST framework will coerce a blank string to None for relational fields for this very reason. Alternatively, you can use JSON and actually send None or null, which should work without issues.

5👍

In addition to what Kevin already said, you can force the APIClient to send JSON using the parameter format='json'.

See the documentation.

4👍

In addition to existing answers,
if you are expecting a null, this probably means you expect your api to receive json.

If that’s the case, you may want to configure the test request default format to json instead of form-data:

In your setting.py:

REST_FRAMEWORK = {
    ...
    'TEST_REQUEST_DEFAULT_FORMAT': 'json'
}

This way, no need to add format='json' to each request

Leave a comment