diff options
| author | Tim Graham <timograham@gmail.com> | 2017-03-17 11:25:12 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-03-21 11:32:43 -0400 |
| commit | e5880516f90773d653a67e95b9b942229e8e9021 (patch) | |
| tree | 8dfdd4811281cae1c75e62a372eb0453796e08cc /django | |
| parent | 5310106ee3c1f5403ceeca9ee09fc92e5e353fa5 (diff) | |
[1.11.x] Fixed #27915 -- Allowed Meta.indexes to be defined in abstract models.
Thanks Markus Holtermann for review.
Backport of 3d19d1428a05b514afb771b52870d1f7c25670d1 from master
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/migrations/state.py | 5 | ||||
| -rw-r--r-- | django/db/models/base.py | 14 | ||||
| -rw-r--r-- | django/db/models/indexes.py | 5 |
3 files changed, 18 insertions, 6 deletions
diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index e44f737106..b6540f5f10 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -460,6 +460,11 @@ class ModelState(object): elif name == "index_together": it = model._meta.original_attrs["index_together"] options[name] = set(normalize_together(it)) + elif name == "indexes": + indexes = [idx.clone() for idx in model._meta.indexes] + for index in indexes: + index.set_name_with_model(model) + options['indexes'] = indexes else: options[name] = model._meta.original_attrs[name] # Force-convert all options to text_type (#23226) diff --git a/django/db/models/base.py b/django/db/models/base.py index 53b761f45a..217bde1e2a 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -303,12 +303,14 @@ class ModelBase(type): else: new_class.add_to_class(field.name, copy.deepcopy(field)) - # 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. - for index in new_class._meta.indexes: - if not index.name: - index.set_name_with_model(new_class) + if base_meta and base_meta.abstract and not abstract: + 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. + for index in new_class._meta.indexes: + if not index.name: + index.set_name_with_model(new_class) if abstract: # Abstract base models can't be instantiated and don't appear in diff --git a/django/db/models/indexes.py b/django/db/models/indexes.py index 1ef5a4ab50..0ee2bf3610 100644 --- a/django/db/models/indexes.py +++ b/django/db/models/indexes.py @@ -77,6 +77,11 @@ class Index(object): path = path.replace('django.db.models.indexes', 'django.db.models') return (path, (), {'fields': self.fields, 'name': self.name}) + def clone(self): + """Create a copy of this Index.""" + path, args, kwargs = self.deconstruct() + return self.__class__(*args, **kwargs) + @staticmethod def _hash_generator(*args): """ |
