Ticket #5000: table-prefix-better.diff

File table-prefix-better.diff, 2.3 KB (added by fotinakis, 15 years ago)

Patch for table prefixing and docs

  • django/db/models/options.py

     
    9898            self.verbose_name_plural = string_concat(self.verbose_name, 's')
    9999        del self.meta
    100100
    101         # If the db_table wasn't provided, use the app_label + module_name.
     101        prefix = getattr(settings, 'DATABASE_TABLE_PREFIX', '')
     102
     103        # If the db_table wasn't provided, use prefix + app_label + module_name.
     104        # Otherwise, use prefix + given table name from db_table.
    102105        if not self.db_table:
    103             self.db_table = "%s_%s" % (self.app_label, self.module_name)
    104             self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
     106            self.db_table = "%s%s_%s" % (prefix, self.app_label, self.module_name)
     107        else:
     108            self.db_table = "%s%s" % (prefix, self.db_table)
     109       
     110        self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
    105111
    106 
    107112    def _prepare(self, model):
    108113        if self.order_with_respect_to:
    109114            self.order_with_respect_to = self.get_field(self.order_with_respect_to)
  • django/conf/global_settings.py

     
    131131DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
    132132DATABASE_OPTIONS = {}          # Set to empty dictionary for default.
    133133
     134# Global prefix for every table created from a model in this project.
     135DATABASE_TABLE_PREFIX = ''
     136
    134137# Host for sending e-mail.
    135138EMAIL_HOST = 'localhost'
    136139
  • docs/ref/settings.txt

     
    227227The port to use when connecting to the database. An empty string means the
    228228default port. Not used with SQLite.
    229229
     230.. setting:: DATABASE_TABLE_PREFIX
     231
     232DATABASE_TABLE_PREFIX
     233---------------------
     234
     235Default: ``''`` (Empty string)
     236
     237A prefix that will be added to every table name. Includes models that
     238specify a table name manually.
     239
    230240.. setting:: DATABASE_USER
    231241
    232242DATABASE_USER
Back to Top