summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Pellerin <jpellerin@gmail.com>2006-07-19 02:43:38 +0000
committerJason Pellerin <jpellerin@gmail.com>2006-07-19 02:43:38 +0000
commitf51a9fa30c7a4978887483bb4d5780d920cc4e9c (patch)
tree7f07f6dbd9dbe116b36c618c481345ca928acad5
parenta5caa0ab27bc5a72b4080888c3f295ee7c9b719a (diff)
[multi-db] Updated multiple_databases tests to use OTHER_DATABASES setting.
git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3369 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--tests/modeltests/multiple_databases/models.py56
1 files changed, 20 insertions, 36 deletions
diff --git a/tests/modeltests/multiple_databases/models.py b/tests/modeltests/multiple_databases/models.py
index b9a1939d41..e811672666 100644
--- a/tests/modeltests/multiple_databases/models.py
+++ b/tests/modeltests/multiple_databases/models.py
@@ -7,9 +7,13 @@ connections. Multiple database support is entirely optional and has
no impact on your application if you don't use it.
Named connections are defined in your settings module. Create a
-`DATABASES` variable that is a dict, mapping connection names to their
+`OTHER_DATABASES` variable that is a dict, mapping connection names to their
particulars. The particulars are defined in a dict with the same keys
-as the variable names as are used to define the default connection.
+as the variable names as are used to define the default connection, with one
+addition: MODELS.
+
+The MODELS item in an OTHER_DATABASES entry is a list of the apps and models
+that will use that connection.
Access to named connections is through `django.db.connections`, which
behaves like a dict: you access connections by name. Connections are
@@ -18,14 +22,10 @@ holds a `ConnectionInfo` instance, with the attributes:
`DatabaseError`, `backend`, `get_introspection_module`,
`get_creation_module`, and `runshell`.
-Models can define which connection to use, by name. To use a named
-connection, set the `db_connection` property in the model's Meta class
-to the name of the connection. The name used must be a key in
-settings.DATABASES, of course.
-
-To access a model's connection, use `model._meta.connection`. To find
-the backend or other connection metadata, use
-`model._meta.connection_info`.
+To access a model's connection, use its manager. The connection is available
+at `model._default_manager.db.connection`. To find the backend or other
+connection metadata, use `model._meta.db` to access the full ConnectionInfo
+with connection metadata.
"""
from django.db import models
@@ -36,10 +36,8 @@ class Artist(models.Model):
def __str__(self):
return self.name
-
- class Meta:
- db_connection = 'django_test_db_a'
+
class Opus(models.Model):
artist = models.ForeignKey(Artist)
name = models.CharField(maxlength=100)
@@ -47,9 +45,6 @@ class Opus(models.Model):
def __str__(self):
return "%s (%s)" % (self.name, self.year)
-
- class Meta:
- db_connection = 'django_test_db_a'
class Widget(models.Model):
@@ -59,9 +54,6 @@ class Widget(models.Model):
def __str__(self):
return self.code
- class Meta:
- db_connection = 'django_test_db_b'
-
class DooHickey(models.Model):
name = models.CharField(maxlength=50)
@@ -69,9 +61,6 @@ class DooHickey(models.Model):
def __str__(self):
return self.name
-
- class Meta:
- db_connection = 'django_test_db_b'
class Vehicle(models.Model):
@@ -92,11 +81,11 @@ API_TESTS = """
['django_test_db_a', 'django_test_db_b']
# Each connection references its settings
->>> connections['django_test_db_a'].settings.DATABASE_NAME == settings.DATABASES['django_test_db_a']['DATABASE_NAME']
+>>> connections['django_test_db_a'].settings.DATABASE_NAME == settings.OTHER_DATABASES['django_test_db_a']['DATABASE_NAME']
True
->>> connections['django_test_db_b'].settings.DATABASE_NAME == settings.DATABASES['django_test_db_b']['DATABASE_NAME']
+>>> connections['django_test_db_b'].settings.DATABASE_NAME == settings.OTHER_DATABASES['django_test_db_b']['DATABASE_NAME']
True
->>> connections['django_test_db_b'].settings.DATABASE_NAME == settings.DATABASES['django_test_db_a']['DATABASE_NAME']
+>>> connections['django_test_db_b'].settings.DATABASE_NAME == settings.OTHER_DATABASES['django_test_db_a']['DATABASE_NAME']
False
# Invalid connection names raise ImproperlyConfigured
@@ -105,20 +94,17 @@ Traceback (most recent call last):
...
ImproperlyConfigured: No database connection 'bad' has been configured
-# Models can access their connections through their _meta properties
->>> Artist._meta.connection.settings == connections['django_test_db_a'].settings
+# Models can access their connections through their managers
+>>> Artist.objects.db == connections['django_test_db_a']
True
->>> Widget._meta.connection.settings == connections['django_test_db_b'].settings
+>>> Widget.objects.db == connections['django_test_db_b']
True
->>> Vehicle._meta.connection.settings == connection.settings
+>>> Vehicle.objects.db.connection == connection
True
->>> Artist._meta.connection.settings == Widget._meta.connection.settings
+>>> Artist.objects.db == Widget.objects.db
False
->>> Artist._meta.connection.settings == Vehicle._meta.connection.settings
+>>> Artist.objects.db.connection == Vehicle.objects.db.connection
False
-
-# Managers use their models' connections
-
>>> a = Artist(name="Paul Klee", alive=False)
>>> a.save()
>>> w = Widget(code='100x2r', weight=1000)
@@ -128,8 +114,6 @@ False
>>> artists = Artist.objects.all()
>>> list(artists)
[<Artist: Paul Klee>]
->>> artists[0]._meta.connection.settings == connections['django_test_db_a'].settings
-True
# When transactions are not managed, model save will commit only
# for the model's connection.