summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/db/models/indexes.py2
-rw-r--r--tests/model_indexes/tests.py5
2 files changed, 7 insertions, 0 deletions
diff --git a/django/db/models/indexes.py b/django/db/models/indexes.py
index c5fb5789fe..fddde3b16f 100644
--- a/django/db/models/indexes.py
+++ b/django/db/models/indexes.py
@@ -35,6 +35,8 @@ class Index:
raise ValueError('Index.fields and Index.opclasses must have the same number of elements.')
if not fields:
raise ValueError('At least one field is required to define an index.')
+ if not all(isinstance(field, str) for field in fields):
+ raise ValueError('Index.fields must contain only strings with field names.')
if include and not name:
raise ValueError('A covering index must be named.')
if not isinstance(include, (type(None), list, tuple)):
diff --git a/tests/model_indexes/tests.py b/tests/model_indexes/tests.py
index 93ac47130a..1fe283340e 100644
--- a/tests/model_indexes/tests.py
+++ b/tests/model_indexes/tests.py
@@ -55,6 +55,11 @@ class SimpleIndexesTests(SimpleTestCase):
with self.assertRaisesMessage(ValueError, 'Index.fields must be a list or tuple.'):
models.Index(fields='title')
+ def test_index_fields_strings(self):
+ msg = 'Index.fields must contain only strings with field names.'
+ with self.assertRaisesMessage(ValueError, msg):
+ models.Index(fields=[models.F('title')])
+
def test_fields_tuple(self):
self.assertEqual(models.Index(fields=('title',)).fields, ['title'])