Commits

Anonymous committed 7f81fed

[soc2009/multidb] Modified using= arguments to default to None; modified querysets so you can track explicit database assignments. Patch from Russell Keith-Magee.

  • Participants
  • Parent commits 5a59fb3
  • Branches soc2009/multidb

Comments (0)

Files changed (8)

File django/contrib/auth/models.py

     pass
 
 class PermissionManager(models.Manager):
-    def get_by_natural_key(self, codename, app_label, model, using=DEFAULT_DB_ALIAS):
+    def get_by_natural_key(self, codename, app_label, model, using=None):
         return self.using(using).get(
             codename=codename,
             content_type=ContentType.objects.get_by_natural_key(app_label, model)
         return self.name
 
 class UserManager(models.Manager):
-    def create_user(self, username, email, password=None, using=DEFAULT_DB_ALIAS):
+    def create_user(self, username, email, password=None, using=None):
         "Creates and saves a User with the given username, e-mail and password."
         now = datetime.datetime.now()
         user = self.model(None, username, '', '', email.strip().lower(), 'placeholder', False, True, False, now, now)
         user.save(using=using)
         return user
 
-    def create_superuser(self, username, email, password, using=DEFAULT_DB_ALIAS):
+    def create_superuser(self, username, email, password, using=None):
         u = self.create_user(username, email, password)
         u.is_staff = True
         u.is_active = True

File django/contrib/gis/sitemaps/views.py

     xml = smart_str(loader.render_to_string('gis/sitemaps/geo_sitemap.xml', {'urlset': urls}))
     return HttpResponse(xml, mimetype='application/xml')
 
-def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS):
+def kml(request, label, model, field_name=None, compress=False, using=None):
     """
     This view generates KML for the given app label, model, and field name.
 
 
     if connection.ops.postgis:
         # PostGIS will take care of transformation.
-        placemarks = klass._default_manager.kml(field_name=field_name)
+        placemarks = klass._default_manager.using(using).kml(field_name=field_name)
     else:
         # There's no KML method on Oracle or MySQL, so we use the `kml`
         # attribute of the lazy geometry instead.
         placemarks = []
         if connection.ops.oracle:
-            qs = klass._default_manager.transform(4326, field_name=field_name)
+            qs = klass._default_manager.using(using).transform(4326, field_name=field_name)
         else:
-            qs = klass._default_manager.all()
+            qs = klass._default_manager.using(using).all()
         for mod in qs:
             setattr(mod, 'kml', getattr(mod, field_name).kml)
             placemarks.append(mod)
         render = render_to_kml
     return render('gis/kml/placemarks.kml', {'places' : placemarks})
 
-def kmz(request, label, model, field_name=None, using=DEFAULT_DB_ALIAS):
+def kmz(request, label, model, field_name=None, using=None):
     """
     This view returns KMZ for the given app label, model, and field name.
     """

File django/contrib/gis/utils/layermapping.py

     def __init__(self, model, data, mapping, layer=0,
                  source_srs=None, encoding=None,
                  transaction_mode='commit_on_success',
-                 transform=True, unique=None, using=DEFAULT_DB_ALIAS):
+                 transform=True, unique=None, using=None):
         """
         A LayerMapping object is initialized using the given Model (not an instance),
         a DataSource (or string path to an OGR-supported data file), and a mapping

File django/db/models/query.py

     def __init__(self, model=None, query=None, using=None):
         self.model = model
         # EmptyQuerySet instantiates QuerySet with model as None
-        self.db = using or DEFAULT_DB_ALIAS
+        self._db = using
         self.query = query or sql.Query(self.model)
         self._result_cache = None
         self._iter = None
         Selects which database this QuerySet should excecute it's query against.
         """
         clone = self._clone()
-        clone.db = alias
+        clone._db = alias
         return clone
 
     ###################################
             return False
     ordered = property(ordered)
 
+    def db(self):
+        "Return the database that will be used if this query is executed now"
+        return self._db or DEFAULT_DB_ALIAS
+    db = property(db)
+
     ###################
     # PRIVATE METHODS #
     ###################
         if self._sticky_filter:
             query.filter_is_sticky = True
         c = klass(model=self.model, query=query)
-        c.db = self.db
+        c._db = self._db
         c.__dict__.update(kwargs)
         if setup and hasattr(c, '_setup_query'):
             c._setup_query()

File tests/modeltests/fixtures/models.py

                                          self.tagged, self.name)
 
 class PersonManager(models.Manager):
-    def get_by_natural_key(self, name, using=DEFAULT_DB_ALIAS):
+    def get_by_natural_key(self, name, using=None):
         return self.using(using).get(name=name)
 
 class Person(models.Model):

File tests/regressiontests/fixtures_regress/models.py

 # Check for forward references in FKs and M2Ms with natural keys
 
 class TestManager(models.Manager):
-    def get_by_natural_key(self, key, using=DEFAULT_DB_ALIAS):
+    def get_by_natural_key(self, key, using=None):
         return self.using(using).get(name=key)
 
 class Store(models.Model):

File tests/regressiontests/generic_inline_admin/tests.py

         self.assertEquals(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="text" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="2" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>')
         self.assertEquals(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>')
 
-
     def testGenericInlineFormsetFactory(self):
         # Regression test for #10522.
         inline_formset = generic_inlineformset_factory(Media,

File tests/regressiontests/multiple_database/models.py

         ordering = ('source',)
 
 class PersonManager(models.Manager):
-    def get_by_natural_key(self, name, using=DEFAULT_DB_ALIAS):
+    def get_by_natural_key(self, name, using=None):
         return self.using(using).get(name=name)
 
 class Person(models.Model):