Browse Source

Refs #32460 -- Doc'd and tested that property names of model choice enums cannot be used as members.

Nick Pope 4 years ago
parent
commit
41e39c41c9
2 changed files with 9 additions and 0 deletions
  1. 5 0
      docs/ref/models/fields.txt
  2. 4 0
      tests/model_enums/tests.py

+ 5 - 0
docs/ref/models/fields.txt

@@ -226,6 +226,11 @@ modifications:
   ``.choices``, ``.labels``, ``.values``, and ``.names`` -- to make it easier
   to access lists of those separate parts of the enumeration. Use ``.choices``
   as a suitable value to pass to :attr:`~Field.choices` in a field definition.
+
+  .. warning::
+
+    These property names cannot be used as member names as they would conflict.
+
 * The use of :func:`enum.unique()` is enforced to ensure that values cannot be
   defined multiple times. This is unlikely to be expected in choices for a
   field.

+ 4 - 0
tests/model_enums/tests.py

@@ -155,6 +155,10 @@ class ChoicesTests(SimpleTestCase):
         output = template.render(Context({'Suit': Suit}))
         self.assertEqual(output, 'Diamond|1')
 
+    def test_property_names_conflict_with_member_names(self):
+        with self.assertRaises(AttributeError):
+            models.TextChoices('Properties', 'choices labels names values')
+
 
 class Separator(bytes, models.Choices):
     FS = b'\x1c', 'File Separator'