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,14 +293,15 @@ class ModelBase(type):
else: else:
new_class.add_to_class(field.name, copy.deepcopy(field)) 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
new_class._meta.indexes = [copy.deepcopy(idx) for idx in new_class._meta.indexes] # abstract model.
# Set the name of _meta.indexes. This can't be done in new_class._meta.indexes = [copy.deepcopy(idx) for idx in new_class._meta.indexes]
# Options.contribute_to_class() because fields haven't been added # Set the name of _meta.indexes. This can't be done in
# to the model at that point. # Options.contribute_to_class() because fields haven't been added to
for index in new_class._meta.indexes: # the model at that point.
if not index.name: for index in new_class._meta.indexes:
index.set_name_with_model(new_class) if not index.name:
index.set_name_with_model(new_class)
if abstract: if abstract:
# Abstract base models can't be instantiated and don't appear in # Abstract base models can't be instantiated and don't appear in

View File

@ -23,3 +23,6 @@ Bugfixes
(:ticket:`28202`). (:ticket:`28202`).
* Fixed invalid HTML for a required ``AdminFileWidget`` (:ticket:`28278`). * 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) author = models.CharField(max_length=50)
pages = models.IntegerField(db_column='page_count') pages = models.IntegerField(db_column='page_count')
class Meta:
indexes = [models.indexes.Index(fields=['title'])]
class AbstractModel(models.Model): class AbstractModel(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)

View File

@ -83,6 +83,10 @@ class IndexesTests(SimpleTestCase):
self.assertIsNot(index, new_index) self.assertIsNot(index, new_index)
self.assertEqual(index.fields, new_index.fields) 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): def test_abstract_children(self):
index_names = [index.name for index in ChildModel1._meta.indexes] index_names = [index.name for index in ChildModel1._meta.indexes]
self.assertEqual(index_names, ['model_index_name_440998_idx']) self.assertEqual(index_names, ['model_index_name_440998_idx'])