You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nested ListField serializer errors should include object reference #3274
When validation errors happen in nested ListField serializers -- an OrderedDict is returned for the ListField containing the nested errors. However, it doesn't indicate which object in the list caused the error.
For example, my nested serializer has a URLField, if I pass in a list of 10 objects -- the error doesn't indicate which of the 10 objects contains the error.
classChildSerializer(serializers.Serializer):
url=serializers.URLField()
uuid=serializers.UUIDField()
other_fields=serializers.CharField() # many more...classMySerializer(serializers.ModelSerializer):
# my_nested_list_field is a JSONfield on the Django modelmy_nested_list_field=serializer.ListField(child=ChildSerializer())
data= {#other fields...,'my_nested_listfield': [
{"url": "some invalid url", "other_fields": "MANY"},
{"url": "http://www.google.com/", "other_fields": "MANY"},
#9 (n) more dicts...
]
}
x=MySerializer(data=data)
x.is_valid()
x.errors
{'my_nested_listfield': OrderedDict([('url', [u'Enter a valid URL.'])])}
# did this error occur in dict #1, #5, #10???? Start trawling through the list looking for the bad URL...
Nested errors should probably contain some metadata to indicate which item(s) failed validation so that useful errors can be displayed to the client.
Something like:
{'my_nested_listfield': OrderedDict([('url', [u'Enter a valid URL.'] "_meta_ref": "OBJECT_123")])}
# Oh, I see, the error occured on OBJECT_123 -- this will be quick and easy to debug
Where _meta_ref is generated by an overrideable method on the child serializer. It could default to the object url -- but in this case I would use an internal id that is meaningful to my application, like the uuid
This is a fairly old issue, but I think this use case is generally covered by #5655. In short, there won't be an object _meta_ref (it's not exactly clear what that value should be). Instead, errors will be a dict of errors, keyed by the failing value's index (or in the case of DictField, the value's key). For example:
Activity
lovelydinosaur commentedon Aug 20, 2015
Get the general idea of this, but not clear what a nice way to do this would be.
I'm not super keen on
_meta_refbut don't have any great alternative.rpkilby commentedon Dec 5, 2017
Hi @erikcw - would #5655 solve this for you?
rpkilby commentedon Dec 21, 2017
This is a fairly old issue, but I think this use case is generally covered by #5655. In short, there won't be an object
_meta_ref(it's not exactly clear what that value should be). Instead, errors will be a dict of errors, keyed by the failing value's index (or in the case ofDictField, the value's key). For example: