Django

Code

Changeset 3281

Show
Ignore:
Timestamp:
07/06/06 16:45:52 (2 years ago)
Author:
jpellerin
Message:

[multi-db] Added transaction and relation usage tests to multiple
databases model tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/multiple-db-support/tests/modeltests/multiple_databases/models.py

    r3267 r3281  
    4141        db_connection = 'django_test_db_a' 
    4242 
     43class Opus(models.Model): 
     44    artist = models.ForeignKey(Artist) 
     45    name = models.CharField(maxlength=100) 
     46    year = models.IntegerField() 
     47     
     48    def __str__(self): 
     49        return "%s (%s)" % (self.name, self.year) 
     50     
     51    class Meta: 
     52        db_connection = 'django_test_db_a' 
     53 
     54 
    4355class Widget(models.Model): 
    4456    code = models.CharField(maxlength=10, unique=True) 
     
    5062    class Meta: 
    5163        db_connection = 'django_test_db_b' 
     64 
     65 
     66class DooHickey(models.Model): 
     67    name = models.CharField(maxlength=50) 
     68    widgets = models.ManyToManyField(Widget, related_name='doohickeys') 
     69     
     70    def __str__(self): 
     71        return self.name 
     72     
     73    class Meta: 
     74        db_connection = 'django_test_db_b' 
     75 
    5276 
    5377class Vehicle(models.Model): 
     
    107131>>> artists[0]._meta.connection.settings == connections['django_test_db_a'].settings 
    108132True 
     133 
     134# When not using transaction management, model save will commit only 
     135# for the model's connection. 
     136 
     137>>> from django.db import transaction 
     138>>> transaction.enter_transaction_management() 
     139>>> a = Artist(name="Joan Miro", alive=False) 
     140>>> w = Widget(code="99rbln", weight=1) 
     141>>> a.save() 
     142 
     143# Only connection 'django_test_db_a' is committed, so if we rollback 
     144# all connections we'll forget the new Widget. 
     145 
     146>>> transaction.rollback() 
     147>>> list(Artist.objects.all()) 
     148[<Artist: Paul Klee>, <Artist: Joan Miro>] 
     149>>> list(Widget.objects.all()) 
     150[<Widget: 100x2r>] 
     151 
     152# Managed transaction state applies across all connections. 
     153 
     154>>> transaction.managed(True) 
     155 
     156# When managed, just as when using a single connection, updates are 
     157# not committed until a commit is issued. 
     158 
     159>>> a = Artist(name="Pablo Picasso", alive=False) 
     160>>> a.save() 
     161>>> w = Widget(code="99rbln", weight=1) 
     162>>> w.save() 
     163>>> v = Vehicle(make='Pontiac', model='Fiero', year='1987') 
     164>>> v.save() 
     165 
     166# The connections argument may be passed to commit, rollback, and the 
     167# commit_on_success decorator as a keyword argument, as the first (for 
     168# commit and rollback) or second (for the decorator) positional 
     169# argument. It may be passed as a ConnectionInfo object, a connection 
     170# (DatabaseWrapper) object, a connection name, or a list or dict of 
     171# ConnectionInfo objects, connection objects, or connection names. If a 
     172# dict is passed, the keys are ignored and the values used as the list 
     173# of connections to commit, rollback, etc. 
     174 
     175>>> transaction.commit(connections['django_test_db_b']) 
     176>>> transaction.commit('django_test_db_b') 
     177>>> transaction.commit(connections='django_test_db_b') 
     178>>> transaction.commit(connections=['django_test_db_b']) 
     179>>> transaction.commit(['django_test_db_a', 'django_test_db_b']) 
     180>>> transaction.commit(connections) 
     181 
     182# When the connections argument is omitted entirely, the transaction 
     183# command applies to all connections. Here we have committed 
     184# connections 'django_test_db_a' and 'django_test_db_b', but not the 
     185# default connection, so the new vehicle is lost on rollback. 
     186 
     187>>> transaction.rollback() 
     188>>> list(Artist.objects.all()) 
     189[<Artist: Paul Klee>, <Artist: Joan Miro>, <Artist: Pablo Picasso>] 
     190>>> list(Widget.objects.all()) 
     191[<Widget: 100x2r>, <Widget: 99rbln>] 
     192>>> list(Vehicle.objects.all()) 
     193[<Vehicle: 1966 Chevy Camaro>] 
     194>>> transaction.rollback() 
     195>>> transaction.managed(False) 
     196>>> transaction.leave_transaction_management() 
     197 
     198# Of course, relations and all other normal database operations work 
     199# with models that use named connections just the same as with models 
     200# that use the default connection. The only caveat is that you can't 
     201# use a relation between two models that are stored in different 
     202# databases. Note that that doesn't mean that two models using 
     203# different connection *names* can't be related; only that in the the 
     204# context in which they are used, if you use the relation, the 
     205# connections named by the two models must resolve to the same 
     206# database. 
     207 
     208>>> a = Artist.objects.get(name="Paul Klee") 
     209>>> list(a.opus_set.all()) 
     210[] 
     211>>> a.opus_set.create(name="Magic Garden", year="1926") 
     212<Opus: Magic Garden (1926)> 
     213>>> list(a.opus_set.all()) 
     214[<Opus: Magic Garden (1926)>] 
     215>>> d = DooHickey(name='Thing') 
     216>>> d.save() 
     217>>> d.widgets.create(code='d101', weight=92) 
     218<Widget: d101> 
     219>>> list(d.widgets.all()) 
     220[<Widget: d101>] 
     221>>> w = Widget.objects.get(code='d101') 
     222>>> list(w.doohickeys.all()) 
     223[<DooHickey: Thing>] 
    109224"""