-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unique=True validation not working with models.CharField with choices #5004
Comments
any update on this ? |
Confirmed... >>> from rest_framework.serializers import ModelSerializer
>>> from snippets.models import Poll
>>> class Foo(ModelSerializer):
... class Meta:
... model = Poll
... fields = '__all__'
...
>>> print(Foo())
Foo():
id = IntegerField(label='ID', read_only=True)
form_name = ChoiceField(choices=(('choice1', 'choice 1'), ('choice2', 'choice 1')), label='Name') uniqueness validation has not been applied to the |
Seems like this is the culprit. |
Indeed yup, some careful rejigging will be needed there, since there are some keyword arguments that we don't want to include for |
@tomchristie just removing that early return seems to work since all the checks below have constraint around the field type already.
|
@jpadilla: in #5026 just removing early return may create issues. It will also include validation for |
Fixed via #5028 |
Checklist
master
branch of Django REST framework.Steps to reproduce
Lets take a model
Then making a POST request with
{"form_name": "choice1"}
first time returns{ "id": 1, "form_name": "choice1" }
then by making same request again with data
{"form_name": "choice1"}
instead of giving valid error response, django's IntegrityError exception is thrown.This behaviour is only coming if models.CharField is used with choices and unique=True and not when models.CharField is used without choices
Expected behavior
Valid error response should be returned instead of throwing django's IntegrityError exception
like
{ "form_name": [ "poll with this name already exists." ] }
Actual behavior
Throwing django's IntegrityError exception when models.CharField is used with choices and unique=True
The text was updated successfully, but these errors were encountered: