mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09:13 +00:00
[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
This commit is contained in:
parent
a5caa0ab27
commit
f51a9fa30c
@ -7,9 +7,13 @@ connections. Multiple database support is entirely optional and has
|
|||||||
no impact on your application if you don't use it.
|
no impact on your application if you don't use it.
|
||||||
|
|
||||||
Named connections are defined in your settings module. Create a
|
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
|
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
|
Access to named connections is through `django.db.connections`, which
|
||||||
behaves like a dict: you access connections by name. Connections are
|
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`,
|
`DatabaseError`, `backend`, `get_introspection_module`,
|
||||||
`get_creation_module`, and `runshell`.
|
`get_creation_module`, and `runshell`.
|
||||||
|
|
||||||
Models can define which connection to use, by name. To use a named
|
To access a model's connection, use its manager. The connection is available
|
||||||
connection, set the `db_connection` property in the model's Meta class
|
at `model._default_manager.db.connection`. To find the backend or other
|
||||||
to the name of the connection. The name used must be a key in
|
connection metadata, use `model._meta.db` to access the full ConnectionInfo
|
||||||
settings.DATABASES, of course.
|
with connection metadata.
|
||||||
|
|
||||||
To access a model's connection, use `model._meta.connection`. To find
|
|
||||||
the backend or other connection metadata, use
|
|
||||||
`model._meta.connection_info`.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
@ -36,10 +36,8 @@ class Artist(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
class Meta:
|
|
||||||
db_connection = 'django_test_db_a'
|
|
||||||
|
|
||||||
|
|
||||||
class Opus(models.Model):
|
class Opus(models.Model):
|
||||||
artist = models.ForeignKey(Artist)
|
artist = models.ForeignKey(Artist)
|
||||||
name = models.CharField(maxlength=100)
|
name = models.CharField(maxlength=100)
|
||||||
@ -47,9 +45,6 @@ class Opus(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%s (%s)" % (self.name, self.year)
|
return "%s (%s)" % (self.name, self.year)
|
||||||
|
|
||||||
class Meta:
|
|
||||||
db_connection = 'django_test_db_a'
|
|
||||||
|
|
||||||
|
|
||||||
class Widget(models.Model):
|
class Widget(models.Model):
|
||||||
@ -59,9 +54,6 @@ class Widget(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.code
|
return self.code
|
||||||
|
|
||||||
class Meta:
|
|
||||||
db_connection = 'django_test_db_b'
|
|
||||||
|
|
||||||
|
|
||||||
class DooHickey(models.Model):
|
class DooHickey(models.Model):
|
||||||
name = models.CharField(maxlength=50)
|
name = models.CharField(maxlength=50)
|
||||||
@ -69,9 +61,6 @@ class DooHickey(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
class Meta:
|
|
||||||
db_connection = 'django_test_db_b'
|
|
||||||
|
|
||||||
|
|
||||||
class Vehicle(models.Model):
|
class Vehicle(models.Model):
|
||||||
@ -92,11 +81,11 @@ API_TESTS = """
|
|||||||
['django_test_db_a', 'django_test_db_b']
|
['django_test_db_a', 'django_test_db_b']
|
||||||
|
|
||||||
# Each connection references its settings
|
# 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
|
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
|
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
|
False
|
||||||
|
|
||||||
# Invalid connection names raise ImproperlyConfigured
|
# Invalid connection names raise ImproperlyConfigured
|
||||||
@ -105,20 +94,17 @@ Traceback (most recent call last):
|
|||||||
...
|
...
|
||||||
ImproperlyConfigured: No database connection 'bad' has been configured
|
ImproperlyConfigured: No database connection 'bad' has been configured
|
||||||
|
|
||||||
# Models can access their connections through their _meta properties
|
# Models can access their connections through their managers
|
||||||
>>> Artist._meta.connection.settings == connections['django_test_db_a'].settings
|
>>> Artist.objects.db == connections['django_test_db_a']
|
||||||
True
|
True
|
||||||
>>> Widget._meta.connection.settings == connections['django_test_db_b'].settings
|
>>> Widget.objects.db == connections['django_test_db_b']
|
||||||
True
|
True
|
||||||
>>> Vehicle._meta.connection.settings == connection.settings
|
>>> Vehicle.objects.db.connection == connection
|
||||||
True
|
True
|
||||||
>>> Artist._meta.connection.settings == Widget._meta.connection.settings
|
>>> Artist.objects.db == Widget.objects.db
|
||||||
False
|
False
|
||||||
>>> Artist._meta.connection.settings == Vehicle._meta.connection.settings
|
>>> Artist.objects.db.connection == Vehicle.objects.db.connection
|
||||||
False
|
False
|
||||||
|
|
||||||
# Managers use their models' connections
|
|
||||||
|
|
||||||
>>> a = Artist(name="Paul Klee", alive=False)
|
>>> a = Artist(name="Paul Klee", alive=False)
|
||||||
>>> a.save()
|
>>> a.save()
|
||||||
>>> w = Widget(code='100x2r', weight=1000)
|
>>> w = Widget(code='100x2r', weight=1000)
|
||||||
@ -128,8 +114,6 @@ False
|
|||||||
>>> artists = Artist.objects.all()
|
>>> artists = Artist.objects.all()
|
||||||
>>> list(artists)
|
>>> list(artists)
|
||||||
[<Artist: Paul Klee>]
|
[<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
|
# When transactions are not managed, model save will commit only
|
||||||
# for the model's connection.
|
# for the model's connection.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user