#26609: Add system check for field choices using unorderable iterables
-------------------------------------+-------------------------------------
Reporter: Harry Percival | Owner: Karl
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: migrations | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia Bidart):
Karl: as for the error code, I would use the same code `E004` by
strengthening the guard in the `if` condition, and I guess checking
against `set` is reasonable (system checks are best effort):
{{{#!diff
diff --git a/django/db/models/fields/__init__.py
b/django/db/models/fields/__init__.py
index d1f31f0211..add7236a3a 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -318,11 +318,11 @@ class Field(RegisterLookupMixin):
if not self.choices:
return []
- if not isinstance(self.choices, Iterable) or
isinstance(self.choices, str):
+ if not isinstance(self.choices, Iterable) or
isinstance(self.choices, (str, set)):
return [
checks.Error(
- "'choices' must be a mapping (e.g. a dictionary) or
an iterable "
- "(e.g. a list or tuple).",
+ "'choices' must be a mapping (e.g. a dictionary) or
an stable iterable "
+ "(e.g. a list or tuple, but not set).",
obj=self,
id="fields.E004",
)
}}}
Test are needed though! See
tests/invalid_models_tests/test_ordinary_fields.py.
Also please note that docs/ref/checks.txt will need updating.
--
Ticket URL: <https://code.djangoproject.com/ticket/26609#comment:11>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/01070192ba7183eb-f30ad9b0-41b5-4144-9af8-ff4597e2f20c-000000%40eu-central-1.amazonses.com.