我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用django.db.models.Index()。
def set_name_with_model(self, model): """Sets an unique generated name for the index. PartialIndex would like to only override "hash_data = ...", but the entire method must be duplicated for that. """ table_name = model._meta.db_table column_names = [model._meta.get_field(field_name).column for field_name, order in self.fields_orders] column_names_with_order = [ (('-%s' if order else '%s') % column_name) for column_name, (field_name, order) in zip(column_names, self.fields_orders) ] # The length of the parts of the name is based on the default max # length of 30 characters. hash_data = [table_name] + column_names_with_order + [self.suffix] + self.name_hash_extra_data() self.name = '%s_%s_%s' % ( table_name[:11], column_names[0][:7], '%s_%s' % (self._hash_generator(*hash_data), self.suffix), ) assert len(self.name) <= self.max_name_length, ( 'Index too long for multiple database support. Is self.suffix ' 'longer than 3 characters?' ) self.check_name()