Validate that ChoiceField choices are unique, and values submitted only once
- Dominant language
- No language data
- Stars
- 188
- Forks
- 7
- PR merge metrics
- No merged PRs in 30d
Description
### Code of Conduct
- [x] I agree to follow Django's 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:
```python
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:
```pycon
>>> 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.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.