def create_index_query(self, model_class, field_names, unique, framing=None): framing = framing or 'CREATE %(unique)s INDEX %(index)s ON %(table)s(%(field)s);' if isinstance(field_names, basestring): field_names = (field_names,) columns = [] for field_name in field_names: if field_name not in model_class._meta.fields: raise AttributeError( 'Field %s not on model %s' % (field_name, model_class) ) else: field_obj = model_class._meta.fields[field_name] columns.append(field_obj.db_column) db_table = model_class._meta.db_table index_name = self.quote_name('%s_%s' % (db_table, '_'.join(columns))) unique_expr = ternary(unique, 'UNIQUE', '') return framing % { 'unique': unique_expr, 'index': index_name, 'table': self.quote_name(db_table), 'field': ','.join(map(self.quote_name, columns)), }
def render_field_template(self, quote_char=''): params = { 'column': self.column.render(self.model._meta.database), 'nullable': ternary(self.null, '', ' NOT NULL'), 'qc': quote_char, } params.update(self.column.attributes) return self.field_template % params