Ticket #32231: params_none.diff

File params_none.diff, 4.3 KB (added by Alexander Lyabah, 4 years ago)
  • django/db/backends/utils.py

    diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py
    index 597e6b3a9d..a2271831d5 100644
    a b class CursorWrapper:  
    4545    # The following methods cannot be implemented in __getattr__, because the
    4646    # code must run when the method is invoked, not just when it is accessed.
    4747
    48     def callproc(self, procname, params=None, kparams=None):
     48    def callproc(self, procname, params=(), kparams=None):
    4949        # Keyword parameters for callproc aren't supported in PEP 249, but the
    5050        # database driver may support them (e.g. cx_Oracle).
    5151        if kparams is not None and not self.db.features.supports_callproc_kwargs:
    class CursorWrapper:  
    6060            elif kparams is None:
    6161                return self.cursor.callproc(procname, params)
    6262            else:
    63                 params = params or ()
    6463                return self.cursor.callproc(procname, params, kparams)
    6564
    6665    def execute(self, sql, params=None):
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index ffd8626f4b..2469edf81e 100644
    a b class QuerySet:  
    778778    # PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS #
    779779    ##################################################
    780780
    781     def raw(self, raw_query, params=None, translations=None, using=None):
     781    def raw(self, raw_query, params=(), translations=None, using=None):
    782782        if using is None:
    783783            using = self.db
    784784        qs = RawQuerySet(raw_query, model=self.model, params=params, translations=translations, using=using)
    class RawQuerySet:  
    13321332    Provide an iterator which converts the results of raw SQL queries into
    13331333    annotated model instances.
    13341334    """
    1335     def __init__(self, raw_query, model=None, query=None, params=None,
     1335    def __init__(self, raw_query, model=None, query=None, params=(),
    13361336                 translations=None, using=None, hints=None):
    13371337        self.raw_query = raw_query
    13381338        self.model = model
    13391339        self._db = using
    13401340        self._hints = hints or {}
    13411341        self.query = query or sql.RawQuery(sql=raw_query, using=self.db, params=params)
    1342         self.params = params or ()
     1342        self.params = params
    13431343        self.translations = translations or {}
    13441344        self._result_cache = None
    13451345        self._prefetch_related_lookups = ()
  • django/db/models/sql/query.py

    diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
    index b99f0e90ef..0a4cddbf83 100644
    a b def _get_col(target, field, alias, simple_col):  
    7171class RawQuery:
    7272    """A single raw SQL query."""
    7373
    74     def __init__(self, sql, using, params=None):
    75         self.params = params or ()
     74    def __init__(self, sql, using, params=()):
     75        self.params = params
    7676        self.sql = sql
    7777        self.using = using
    7878        self.cursor = None
    class RawQuery:  
    113113
    114114    @property
    115115    def params_type(self):
     116        if self.params is None:
     117            return None
    116118        return dict if isinstance(self.params, Mapping) else tuple
    117119
    118120    def __str__(self):
     121        if self.params_type is None:
     122            return self.sql
    119123        return self.sql % self.params_type(self.params)
    120124
    121125    def _execute_query(self):
    class RawQuery:  
    129133            params = tuple(adapter(val) for val in self.params)
    130134        elif params_type is dict:
    131135            params = {key: adapter(val) for key, val in self.params.items()}
     136        elif params_type is None:
     137            params = None
    132138        else:
    133139            raise RuntimeError("Unexpected params type: %s" % params_type)
    134140
  • tests/raw_query/tests.py

    diff --git a/tests/raw_query/tests.py b/tests/raw_query/tests.py
    index 703a6b311e..e62d752fe5 100644
    a b class RawQueryTests(TestCase):  
    180180        self.assertEqual(len(results), 1)
    181181        self.assertIsInstance(repr(qset), str)
    182182
     183    def test_like_param(self):
     184        query = "SELECT * FROM raw_query_author WHERE first_name like 'J%'"
     185        qset = Author.objects.raw(query, params=None)
     186        results = list(qset)
     187        self.assertEqual(len(results), 2)
     188
    183189    @skipUnlessDBFeature('supports_paramstyle_pyformat')
    184190    def test_pyformat_params(self):
    185191        """
Back to Top