Code of Conduct
Feature Description
This suggestion is 2 very closely related suggestions, which likely could be fixed together.
- The
choices argument to ChoiceField (and subclasses) should be validated as unique.
- If duplicate choices are submitted to a
MultipleChoiceField (and subclasses), input should be either rejected or deduplicated.
Problem
When defining a ChoiceField (or subclass), its choices can contain duplicates:
MultipleChoiceField(choices=[("1", 1), ("1", 2)])
In this case, the first match in choices is used, which is at least deterministic.
Additionally, MultipleChoiceField allows duplication of submissions:
>>> f = MultipleChoiceField(choices=[("1", 1), ("2", 2)])
>>> f.clean(["1", "1"])
['1', '1']
This could be surprising, if a user expects values to already be validated as unique.
Request or proposal
proposal
Additional Details
Discovered as part of https://code.djangoproject.com/ticket/36913
Implementation Suggestions
Validating choices are unique should be a non-breaking change, since it would already not work as expected.
Requiring unique submissions (or stripping out duplicates) could be breaking if someone is depending on the functionality. It would be sad if the behaviour had to be opt-in, since there are performance benefits from assuming submissions are unique.
Code of Conduct
Feature Description
This suggestion is 2 very closely related suggestions, which likely could be fixed together.
choicesargument toChoiceField(and subclasses) should be validated as unique.MultipleChoiceField(and subclasses), input should be either rejected or deduplicated.Problem
When defining a
ChoiceField(or subclass), itschoicescan contain duplicates:In this case, the first match in
choicesis used, which is at least deterministic.Additionally,
MultipleChoiceFieldallows duplication of submissions:This could be surprising, if a user expects values to already be validated as unique.
Request or proposal
proposal
Additional Details
Discovered as part of https://code.djangoproject.com/ticket/36913
Implementation Suggestions
Validating choices are unique should be a non-breaking change, since it would already not work as expected.
Requiring unique submissions (or stripping out duplicates) could be breaking if someone is depending on the functionality. It would be sad if the behaviour had to be opt-in, since there are performance benefits from assuming submissions are unique.