1
0
mirror of https://github.com/django/django.git synced 2025-07-05 02:09:13 +00:00

[soc2009/multidb] Fixed a bug with EmptyQuerySet.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11257 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2009-07-17 00:15:02 +00:00
parent 24b66ace9d
commit 6060ee7c77
6 changed files with 22 additions and 5 deletions

View File

@ -2,6 +2,7 @@ from optparse import make_option
from django.core.management.base import LabelCommand
from django.db import connections, transaction, models, DEFAULT_DB_ALIAS
from django.db.utils import call_with_connection
class Command(LabelCommand):
help = "Creates the table needed to use the SQL cache backend."
@ -30,7 +31,7 @@ class Command(LabelCommand):
index_output = []
qn = connection.ops.quote_name
for f in fields:
field_output = [qn(f.name), f.db_type(connection)]
field_output = [qn(f.name), call_with_connection(f.db_type, connection=connection)]
field_output.append("%sNULL" % (not f.null and "NOT " or ""))
if f.primary_key:
field_output.append("PRIMARY KEY")

View File

@ -8,6 +8,7 @@ except NameError:
from django.conf import settings
from django.core.management import call_command
from django.db.utils import call_with_connection
# The prefix to put on the default database name when creating
# the test database.
@ -47,7 +48,7 @@ class BaseDatabaseCreation(object):
pending_references = {}
qn = self.connection.ops.quote_name
for f in opts.local_fields:
col_type = f.db_type(self.connection)
col_type = call_with_connection(f.db_type, connection=self.connection)
tablespace = f.db_tablespace or opts.db_tablespace
if col_type is None:
# Skip ManyToManyFields, because they're not represented as

View File

@ -5,6 +5,7 @@ from django.db.models.fields import AutoField, Field, IntegerField, PositiveInte
from django.db.models.related import RelatedObject
from django.db.models.query import QuerySet
from django.db.models.query_utils import QueryWrapper
from django.db.utils import call_with_connection
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext_lazy, string_concat, ungettext, ugettext as _
from django.utils.functional import curry
@ -772,7 +773,7 @@ class ForeignKey(RelatedField, Field):
isinstance(rel_field, (PositiveIntegerField,
PositiveSmallIntegerField)))):
return IntegerField().db_type(connection)
return rel_field.db_type(connection)
return call_with_connection(rel_field.db_type, connection=connection)
class OneToOneField(ForeignKey):
"""

View File

@ -33,7 +33,12 @@ class QuerySet(object):
"""
def __init__(self, model=None, query=None):
self.model = model
using = model._meta.using or DEFAULT_DB_ALIAS
# EmptyQuerySet instantiates QuerySet with model as None
if model:
using = model._meta.using
else:
using = None
using = using or DEFAULT_DB_ALIAS
connection = connections[using]
self.query = query or sql.Query(self.model, connection)
self._result_cache = None

View File

@ -6,6 +6,7 @@ import datetime
from django.utils import tree
from django.db.models.fields import Field
from django.db.models.query_utils import QueryWrapper
from django.db.utils import call_with_connection
from datastructures import EmptyResultSet, FullResultSet
# Connection types
@ -266,7 +267,7 @@ class Constraint(object):
try:
if self.field:
params = self.field.get_db_prep_lookup(lookup_type, value)
db_type = self.field.db_type(connection)
db_type = call_with_connection(self.field.db_type, connection=connection)
else:
# This branch is used at times when we add a comparison to NULL
# (we don't really want to waste time looking up the associated

View File

@ -1,3 +1,4 @@
import inspect
import os
from django.conf import settings
@ -31,6 +32,13 @@ def load_backend(backend_name):
else:
raise # If there's some other error, this must be an error in Django itself.
def call_with_connection(func, *args, **kwargs):
arg_names, varargs, varkwargs, defaults = inspect.getargspec(func)
if 'connection' not in arg_names and varkwargs is None:
del kwargs['connection']
return func(*args, **kwargs)
class ConnectionHandler(object):
def __init__(self, databases):
self.databases = databases