mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09:13 +00:00
[soc2009/multidb] Modified using= arguments to default to None; modified 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
This commit is contained in:
parent
562c5ffb07
commit
bdf21ccf8b
@ -48,7 +48,7 @@ class SiteProfileNotAvailable(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
class PermissionManager(models.Manager):
|
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(
|
return self.using(using).get(
|
||||||
codename=codename,
|
codename=codename,
|
||||||
content_type=ContentType.objects.get_by_natural_key(app_label, model)
|
content_type=ContentType.objects.get_by_natural_key(app_label, model)
|
||||||
@ -106,7 +106,7 @@ class Group(models.Model):
|
|||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
class UserManager(models.Manager):
|
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."
|
"Creates and saves a User with the given username, e-mail and password."
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
user = self.model(None, username, '', '', email.strip().lower(), 'placeholder', False, True, False, now, now)
|
user = self.model(None, username, '', '', email.strip().lower(), 'placeholder', False, True, False, now, now)
|
||||||
@ -117,7 +117,7 @@ class UserManager(models.Manager):
|
|||||||
user.save(using=using)
|
user.save(using=using)
|
||||||
return user
|
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 = self.create_user(username, email, password)
|
||||||
u.is_staff = True
|
u.is_staff = True
|
||||||
u.is_active = True
|
u.is_active = True
|
||||||
|
@ -59,7 +59,7 @@ def sitemap(request, sitemaps, section=None):
|
|||||||
xml = smart_str(loader.render_to_string('gis/sitemaps/geo_sitemap.xml', {'urlset': urls}))
|
xml = smart_str(loader.render_to_string('gis/sitemaps/geo_sitemap.xml', {'urlset': urls}))
|
||||||
return HttpResponse(xml, mimetype='application/xml')
|
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.
|
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:
|
if connection.ops.postgis:
|
||||||
# PostGIS will take care of transformation.
|
# 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:
|
else:
|
||||||
# There's no KML method on Oracle or MySQL, so we use the `kml`
|
# There's no KML method on Oracle or MySQL, so we use the `kml`
|
||||||
# attribute of the lazy geometry instead.
|
# attribute of the lazy geometry instead.
|
||||||
placemarks = []
|
placemarks = []
|
||||||
if connection.ops.oracle:
|
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:
|
else:
|
||||||
qs = klass._default_manager.all()
|
qs = klass._default_manager.using(using).all()
|
||||||
for mod in qs:
|
for mod in qs:
|
||||||
setattr(mod, 'kml', getattr(mod, field_name).kml)
|
setattr(mod, 'kml', getattr(mod, field_name).kml)
|
||||||
placemarks.append(mod)
|
placemarks.append(mod)
|
||||||
@ -103,7 +103,7 @@ def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB
|
|||||||
render = render_to_kml
|
render = render_to_kml
|
||||||
return render('gis/kml/placemarks.kml', {'places' : placemarks})
|
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.
|
This view returns KMZ for the given app label, model, and field name.
|
||||||
"""
|
"""
|
||||||
|
@ -67,7 +67,7 @@ class LayerMapping(object):
|
|||||||
def __init__(self, model, data, mapping, layer=0,
|
def __init__(self, model, data, mapping, layer=0,
|
||||||
source_srs=None, encoding=None,
|
source_srs=None, encoding=None,
|
||||||
transaction_mode='commit_on_success',
|
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 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
|
a DataSource (or string path to an OGR-supported data file), and a mapping
|
||||||
|
@ -28,7 +28,7 @@ class QuerySet(object):
|
|||||||
def __init__(self, model=None, query=None, using=None):
|
def __init__(self, model=None, query=None, using=None):
|
||||||
self.model = model
|
self.model = model
|
||||||
# EmptyQuerySet instantiates QuerySet with model as None
|
# 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.query = query or sql.Query(self.model)
|
||||||
self._result_cache = None
|
self._result_cache = None
|
||||||
self._iter = None
|
self._iter = None
|
||||||
@ -688,7 +688,7 @@ class QuerySet(object):
|
|||||||
Selects which database this QuerySet should excecute it's query against.
|
Selects which database this QuerySet should excecute it's query against.
|
||||||
"""
|
"""
|
||||||
clone = self._clone()
|
clone = self._clone()
|
||||||
clone.db = alias
|
clone._db = alias
|
||||||
return clone
|
return clone
|
||||||
|
|
||||||
###################################
|
###################################
|
||||||
@ -708,6 +708,11 @@ class QuerySet(object):
|
|||||||
return False
|
return False
|
||||||
ordered = property(ordered)
|
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 #
|
# PRIVATE METHODS #
|
||||||
###################
|
###################
|
||||||
@ -719,7 +724,7 @@ class QuerySet(object):
|
|||||||
if self._sticky_filter:
|
if self._sticky_filter:
|
||||||
query.filter_is_sticky = True
|
query.filter_is_sticky = True
|
||||||
c = klass(model=self.model, query=query)
|
c = klass(model=self.model, query=query)
|
||||||
c.db = self.db
|
c._db = self._db
|
||||||
c.__dict__.update(kwargs)
|
c.__dict__.update(kwargs)
|
||||||
if setup and hasattr(c, '_setup_query'):
|
if setup and hasattr(c, '_setup_query'):
|
||||||
c._setup_query()
|
c._setup_query()
|
||||||
|
@ -57,7 +57,7 @@ class Tag(models.Model):
|
|||||||
self.tagged, self.name)
|
self.tagged, self.name)
|
||||||
|
|
||||||
class PersonManager(models.Manager):
|
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)
|
return self.using(using).get(name=name)
|
||||||
|
|
||||||
class Person(models.Model):
|
class Person(models.Model):
|
||||||
|
@ -83,7 +83,7 @@ class WidgetProxy(Widget):
|
|||||||
# Check for forward references in FKs and M2Ms with natural keys
|
# Check for forward references in FKs and M2Ms with natural keys
|
||||||
|
|
||||||
class TestManager(models.Manager):
|
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)
|
return self.using(using).get(name=key)
|
||||||
|
|
||||||
class Store(models.Model):
|
class Store(models.Model):
|
||||||
|
@ -107,7 +107,6 @@ class GenericAdminViewTest(TestCase):
|
|||||||
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[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>')
|
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):
|
def testGenericInlineFormsetFactory(self):
|
||||||
# Regression test for #10522.
|
# Regression test for #10522.
|
||||||
inline_formset = generic_inlineformset_factory(Media,
|
inline_formset = generic_inlineformset_factory(Media,
|
||||||
|
@ -17,7 +17,7 @@ class Review(models.Model):
|
|||||||
ordering = ('source',)
|
ordering = ('source',)
|
||||||
|
|
||||||
class PersonManager(models.Manager):
|
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)
|
return self.using(using).get(name=name)
|
||||||
|
|
||||||
class Person(models.Model):
|
class Person(models.Model):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user