Add graceful error-handling when a non-unique value is used for a SlugRelatedField #8962
Unanswered
allynt
asked this question in
Ideas & Suggestions
Replies: 1 comment 1 reply
-
that check would be nice to have. btw, can this be handled in django level? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
At the moment, if you specify a field that is not unique as the "slug_field" of a
SlugRelatedField
and the value you pass into the serializer matches multiple models then DRF will crash. This is in contrast to what happens when the value you pass matches zero models; then DRF handles the error gracefully.I know that the documentation recommends only using unique fields as the "slug_field".
But it would be simple to add one more case to the following code:
django-rest-framework/rest_framework/relations.py
Lines 463 to 466 in 1ce0853
Something like
Obviously there would have to be a new "multiple_objects" entry added to the
default_error_messages
dictionary.This would return a properly formatted 400 response when used correctly in a view instead of the current situation which raises a server error.
Beta Was this translation helpful? Give feedback.
All reactions