tomchristie/django-rest-framework — pull request #906 by avsd (2013-06-03 10:40:24+00:00)
Title: Replace 400 (Bad request) with 422 (Unprocessable entity) in CreateMixin and UpdateMixin

Hi,

Currently if the data being posted is correct JSON but contains incorrect values (such as missing fields or wrong field formats), the CreateMixin and UpdateMixin return status code 400 (BAD REQUEST).

This is incorrect.

According to RFC 4918, there is special code for such case - 422:

> The 422 (Unprocessable Entity) status code means the server
> understands the content type of the request entity (hence a
> 415(Unsupported Media Type) status code is inappropriate), and the
> syntax of the request entity is correct (thus a 400 (Bad Request)
> status code is inappropriate) but was unable to process the contained
> instructions. For example, this error condition may occur if an XML
> request body contains well-formed (i.e., syntactically correct), but
> semantically erroneous, XML instructions.

See https://tools.ietf.org/html/rfc4918#section-11.2

It's better to leave 400 status code for requests with syntactically wrong JSON.

I understand that this patch adds backwards incompatibility. However, I think that the problem it resolves is more important.

Also, it's good practice to return 409 (Conflict) response when it's impossible to create an entity due to unique constraint violation, for example. However, this is better done by raising APIException in serializer.

Thanks!