| 1 | == Multiple Database Support == |
| 2 | |
| 3 | Work is underway on a patch for #1142, which will add support for multiple database connections. This page documents my current plan and progress towards a fully working patch. |
| 4 | |
| 5 | === The plan === |
| 6 | |
| 7 | * Add a new settings variable, DATABASES. DATABASES is a dict of named database connection parameter sets. In each set, the keys are the same as the standard database connection settings variables. |
| 8 | {{{ |
| 9 | #!python |
| 10 | DATABASES = { |
| 11 | 'a': { 'DATABASE_ENGINE': 'sqlite3', |
| 12 | 'DATABASE_NAME': '/tmp/dba.db' |
| 13 | }, |
| 14 | 'b': { 'DATABASE_ENGINE': 'sqlite3', |
| 15 | 'DATABASE_NAME': '/tmp/dbb.db' |
| 16 | }} |
| 17 | }}} |
| 18 | |
| 19 | |
| 20 | |
| 21 | * Add db_connection to Meta. This allows model classes to specify which connection they should use. |
| 22 | {{{ |
| 23 | #!python |
| 24 | class Artist(models.Model): |
| 25 | name = models.CharField(maxlength=100) |
| 26 | alive = models.BooleanField(default=True) |
| 27 | |
| 28 | def __str__(self): |
| 29 | return self.name |
| 30 | |
| 31 | class Meta: |
| 32 | db_connection = 'a' |
| 33 | |
| 34 | class Widget(models.Model): |
| 35 | code = models.CharField(maxlength=10, unique=True) |
| 36 | weight = models.IntegerField() |
| 37 | |
| 38 | def __str__(self): |
| 39 | return self.code |
| 40 | |
| 41 | class Meta: |
| 42 | db_connection = 'b' |
| 43 | }}} |