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
p=Product(title="foobar", attributes={'first': 'value', 'second': None})
p.save() # Save success. No error here!# Now, Let's try with Serializerdata= {"title": "foobar", "attributes": {"first": "value", "second": None}}
q=ProductSerializer(data=data)
q.is_valid()
#> Falseq.errors#> {'attributes': ['This field may not be null.']}
Expected behavior
q is valid
Actual behavior
As you can see, q is invalid with an error {'attributes': ['This field may not be null.']}.
When I remove "second": None from attributes. q is valid
Ideally the error message would be more specific too. (It would be nice if second were mentioned, so I knew where the problem lay, assuming the null=False case.)
Ideally the error message would be more specific too. (It would be nice if second were mentioned, so I knew where the problem lay, assuming the null=False case.)
I see two possible solutions:
Prefix all child errors with some common message. {"attributes": ['Error validating value for key "second": This field may not be null.']}
Collect the errors in a dictionary and re-raising those errors on the field. {"attributes": {"second": ["This field may not be null."]}}
I'm not a fan of the former, as it seems error-prone to try and modify ValidationErrors. eg, the detail could be a string, dict, or list of messages.
The latter approach is simple, since we just need to store the existing error detail without altering it.
probably friendlier for APIs, since you have errors stored by key/index
probably less friendly to HTML forms, but composite fields already have issues with forms anyway.
Activity
rpkilby commentedon Dec 4, 2017
This issue seems valid. Django 1.11 added the ability to store nulls as values in addition to strings.
https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/fields/#hstorefield
carltongibson commentedon Dec 4, 2017
Ideally the error message would be more specific too. (It would be nice if
secondwere mentioned, so I knew where the problem lay, assuming thenull=Falsecase.)dungdm93 commentedon Dec 4, 2017
@rpkilby I see. But It would be nice if Rest framework also support this too.
rpkilby commentedon Dec 5, 2017
I see two possible solutions:
{"attributes": ['Error validating value for key "second": This field may not be null.']}{"attributes": {"second": ["This field may not be null."]}}I'm not a fan of the former, as it seems error-prone to try and modify
ValidationErrors. eg, thedetailcould be a string, dict, or list of messages.The latter approach is simple, since we just need to store the existing error detail without altering it.
carltongibson commentedon Dec 5, 2017
OK, second option looks prettier.
Exactly. We have to live with that. HTML forms are what they are. (Not a reason not to do it.)