Fixed #28282 -- Fixed class-based indexes name for models that only inherit Model.

This commit is contained in:
Jon Dufresne 2017-06-07 07:13:12 -07:00 committed by Tim Graham
parent 085c2f94ec
commit 0c3c37a376
4 changed files with 19 additions and 8 deletions

View File

@ -293,11 +293,12 @@ class ModelBase(type):
else:
new_class.add_to_class(field.name, copy.deepcopy(field))
if base_meta and base_meta.abstract and not abstract:
# Copy indexes so that index names are unique when models extend an
# abstract model.
new_class._meta.indexes = [copy.deepcopy(idx) for idx in new_class._meta.indexes]
# Set the name of _meta.indexes. This can't be done in
# Options.contribute_to_class() because fields haven't been added
# to the model at that point.
# Options.contribute_to_class() because fields haven't been added to
# the model at that point.
for index in new_class._meta.indexes:
if not index.name:
index.set_name_with_model(new_class)

View File

@ -23,3 +23,6 @@ Bugfixes
(:ticket:`28202`).
* Fixed invalid HTML for a required ``AdminFileWidget`` (:ticket:`28278`).
* Fixed model initialization to set the name of class-based model indexes
for models that only inherit ``models.Model`` (:ticket:`28282`).

View File

@ -6,6 +6,9 @@ class Book(models.Model):
author = models.CharField(max_length=50)
pages = models.IntegerField(db_column='page_count')
class Meta:
indexes = [models.indexes.Index(fields=['title'])]
class AbstractModel(models.Model):
name = models.CharField(max_length=50)

View File

@ -83,6 +83,10 @@ class IndexesTests(SimpleTestCase):
self.assertIsNot(index, new_index)
self.assertEqual(index.fields, new_index.fields)
def test_name_set(self):
index_names = [index.name for index in Book._meta.indexes]
self.assertEqual(index_names, ['model_index_title_196f42_idx'])
def test_abstract_children(self):
index_names = [index.name for index in ChildModel1._meta.indexes]
self.assertEqual(index_names, ['model_index_name_440998_idx'])