Improve composite field child errors by rpkilby · Pull Request #5655 · encode/django-rest-framework (original) (raw)

Fixes #3274

This is an extension of the discussion in #5644. Errors raised on composite fields (DictField & ListField) are confusing, since child validation errors don't contain any indication of what index/key failed.

As stated by the django docs, the hstore field accepts nulls as values.

class Product(models.Model): attributes = HStoreField()

class ProductSerializer(serializers.ModelSerializer): class Meta: model = Product fields = ['attributes']

With the below input data, we currently exhibit the following error:

q = ProductSerializer(data={"attributes": {"first": "value", "second": None}}) q.is_valid() q.errors {'attributes': ['This field may not be null.']}

The above error is not helpful, since it fails to mention that this applies to the "second" key. The proposed solution is to nest the errors by index/key.

before:
{'attributes': ['This field may not be null.']}

after:
{'attributes': {'second': ["This field may not be null."]}}

TODO: