Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[soc2009/multidb] Modified using= arguments to default to None; modif…

…ied querysets so you can track explicit database assignments. Patch from Russell Keith-Magee.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11893 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bdf21ccf8b5aaeb6c2bed3ce8673d7d31c627f8d 1 parent 562c5ff
Alex Gaynor alex authored
6 django/contrib/auth/models.py
View
@@ -48,7 +48,7 @@ class SiteProfileNotAvailable(Exception):
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)
@@ -106,7 +106,7 @@ def __unicode__(self):
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)
@@ -117,7 +117,7 @@ def create_user(self, username, email, password=None, using=DEFAULT_DB_ALIAS):
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
10 django/contrib/gis/sitemaps/views.py
View
@@ -59,7 +59,7 @@ def sitemap(request, sitemaps, section=None):
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.
@@ -83,15 +83,15 @@ def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB
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)
@@ -103,7 +103,7 @@ def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB
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.
"""
2  django/contrib/gis/utils/layermapping.py
View
@@ -67,7 +67,7 @@ class LayerMapping(object):
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
11 django/db/models/query.py
View
@@ -28,7 +28,7 @@ class QuerySet(object):
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
@@ -688,7 +688,7 @@ def using(self, alias):
Selects which database this QuerySet should excecute it's query against.
"""
clone = self._clone()
- clone.db = alias
+ clone._db = alias
return clone
###################################
@@ -708,6 +708,11 @@ def ordered(self):
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 #
###################
@@ -719,7 +724,7 @@ def _clone(self, klass=None, setup=False, **kwargs):
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()
2  tests/modeltests/fixtures/models.py
View
@@ -57,7 +57,7 @@ def __unicode__(self):
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):
2  tests/regressiontests/fixtures_regress/models.py
View
@@ -83,7 +83,7 @@ class Meta:
# 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):
1  tests/regressiontests/generic_inline_admin/tests.py
View
@@ -107,7 +107,6 @@ def testGenericInlineFormset(self):
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,
2  tests/regressiontests/multiple_database/models.py
View
@@ -17,7 +17,7 @@ class Meta:
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):
Please sign in to comment.
Something went wrong with that request. Please try again.