26👍
After having the same problem I’d like to highlight the solution offered by @Andrea Corbellini in the comments section:
print(s.errors)
Will return a dictionary containing the fields: reason for fail
. In my case this looked like:
{
'start_date': [ErrorDetail(string='Expected a date but got a datetime.', code='datetime')],
'end_date': [ErrorDetail(string='Expected a date but got a datetime.', code='datetime')],
'client': [ErrorDetail(string='Incorrect type. Expected pk value, received Client.', code='incorrect_type')]
}
I found this absolutely invaluable.
8👍
s = UserSerializer(data=u)
should be:
s = UserSerializer(data={"userId"="user", "email"="asd@gmail.com"})
The serialization process (from the Model to the dictionary) doesn’t require a call to is_valid
:
s = UserSerializer(instance=u)
s.data
The deserialization process (from dict to Model) doesn’t accept Model:
s = UserSerializer(data={"userId"="user", "email"="asd@gmail.com"})
s.is_valid()
s.validated_data
2👍
If you want to see exception details use raise_exception
argument as True
i.e
obj.is_valid(raise_exception=True)
Source:stackexchange.com