tomchristie/django-rest-framework — pull request #1441 by dustinfarris (2014-02-26 19:38:18+00:00)
Title: TestCases and POST/PUT'ing serialized data that contains `None`

As described in #1303, when POST/PUT'ing Python data that contains `None`, e.g.:

```python
serialized_data = {
'id': 5,
'name': 'example',
'related_object': None,
}
```

the expected JSON equivalent of this would be:

```json
{
"id": "5",
"name": "example",
"related_object": ""
}
```

When receiving data from a typical JSON API client, like jQuery, there is no issue translating the related_object field, an empty-string in this case, to a Python `None` literal. However, Django's test [Client](https://github.com/django/django/blob/master/django/test/client.py#L358) (also [subclassed by rest_framework](https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/test.py#L118)'s APIClient) coerces POST data to bytestrings as [seen here](https://github.com/django/django/blob/master/django/test/client.py#L148-L176). `None` is therefore converted to `b'None'`, rather than `""`. When this data is then passed on to rest_framework, a TypeError is encountered, and the following error returned as a 400 response:

```JSON
{"related_object": ["Incorrect type. Expected pk value, received str."]}
```

## This PR contains...

* A failing test that demonstrates the problem

/cc @frankpape