From febe136d4c3310ec8901abecca3ea5ba2be3952c Mon Sep 17 00:00:00 2001 From: can Date: Fri, 5 Jul 2019 15:15:41 +0300 Subject: Fixed #30397 -- Added app_label/class interpolation for names of indexes and constraints. --- django/db/models/options.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'django') diff --git a/django/db/models/options.py b/django/db/models/options.py index fea65f7626..1f11e26d87 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -180,6 +180,12 @@ class Options: self.unique_together = normalize_together(self.unique_together) self.index_together = normalize_together(self.index_together) + # App label/class name interpolation for names of constraints and + # indexes. + if not getattr(cls._meta, 'abstract', False): + for attr_name in {'constraints', 'indexes'}: + objs = getattr(self, attr_name, []) + setattr(self, attr_name, self._format_names_with_class(cls, objs)) # verbose_name_plural is a special case because it uses a 's' # by default. @@ -201,6 +207,18 @@ class Options: self.db_table = "%s_%s" % (self.app_label, self.model_name) self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) + def _format_names_with_class(self, cls, objs): + """App label/class name interpolation for object names.""" + new_objs = [] + for obj in objs: + obj = obj.clone() + obj.name = obj.name % { + 'app_label': cls._meta.app_label.lower(), + 'class': cls.__name__.lower(), + } + new_objs.append(obj) + return new_objs + def _prepare(self, model): if self.order_with_respect_to: # The app registry will not be ready at this point, so we cannot -- cgit v1.3