mirror of https://github.com/django/django.git
Refs #26709 -- Made Index raise ValueError on non-string fields.
This commit is contained in:
parent
c412d9af7e
commit
4c62cdaa10
|
@ -35,6 +35,8 @@ class Index:
|
||||||
raise ValueError('Index.fields and Index.opclasses must have the same number of elements.')
|
raise ValueError('Index.fields and Index.opclasses must have the same number of elements.')
|
||||||
if not fields:
|
if not fields:
|
||||||
raise ValueError('At least one field is required to define an index.')
|
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:
|
if include and not name:
|
||||||
raise ValueError('A covering index must be named.')
|
raise ValueError('A covering index must be named.')
|
||||||
if not isinstance(include, (type(None), list, tuple)):
|
if not isinstance(include, (type(None), list, tuple)):
|
||||||
|
|
|
@ -55,6 +55,11 @@ class SimpleIndexesTests(SimpleTestCase):
|
||||||
with self.assertRaisesMessage(ValueError, 'Index.fields must be a list or tuple.'):
|
with self.assertRaisesMessage(ValueError, 'Index.fields must be a list or tuple.'):
|
||||||
models.Index(fields='title')
|
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):
|
def test_fields_tuple(self):
|
||||||
self.assertEqual(models.Index(fields=('title',)).fields, ['title'])
|
self.assertEqual(models.Index(fields=('title',)).fields, ['title'])
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue