Code

Ticket #5000: table-prefix-better.diff

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

Patch for table prefixing and docs

Line 
1Index: django/db/models/options.py
2===================================================================
3--- django/db/models/options.py (revision 11476)
4+++ django/db/models/options.py (working copy)
5@@ -98,12 +98,17 @@
6             self.verbose_name_plural = string_concat(self.verbose_name, 's')
7         del self.meta
8 
9-        # If the db_table wasn't provided, use the app_label + module_name.
10+        prefix = getattr(settings, 'DATABASE_TABLE_PREFIX', '')
11+
12+        # If the db_table wasn't provided, use prefix + app_label + module_name.
13+        # Otherwise, use prefix + given table name from db_table.
14         if not self.db_table:
15-            self.db_table = "%s_%s" % (self.app_label, self.module_name)
16-            self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
17+            self.db_table = "%s%s_%s" % (prefix, self.app_label, self.module_name)
18+        else:
19+            self.db_table = "%s%s" % (prefix, self.db_table)
20+       
21+        self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
22 
23-
24     def _prepare(self, model):
25         if self.order_with_respect_to:
26             self.order_with_respect_to = self.get_field(self.order_with_respect_to)
27Index: django/conf/global_settings.py
28===================================================================
29--- django/conf/global_settings.py      (revision 11476)
30+++ django/conf/global_settings.py      (working copy)
31@@ -131,6 +131,9 @@
32 DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
33 DATABASE_OPTIONS = {}          # Set to empty dictionary for default.
34 
35+# Global prefix for every table created from a model in this project.
36+DATABASE_TABLE_PREFIX = ''
37+
38 # Host for sending e-mail.
39 EMAIL_HOST = 'localhost'
40 
41Index: docs/ref/settings.txt
42===================================================================
43--- docs/ref/settings.txt       (revision 11476)
44+++ docs/ref/settings.txt       (working copy)
45@@ -227,6 +227,16 @@
46 The port to use when connecting to the database. An empty string means the
47 default port. Not used with SQLite.
48 
49+.. setting:: DATABASE_TABLE_PREFIX
50+
51+DATABASE_TABLE_PREFIX
52+---------------------
53+
54+Default: ``''`` (Empty string)
55+
56+A prefix that will be added to every table name. Includes models that
57+specify a table name manually.
58+
59 .. setting:: DATABASE_USER
60 
61 DATABASE_USER
62