Code

Ticket #14733: 14733-xof.diff

File 14733-xof.diff, 1.8 KB (added by Xof, 3 years ago)

Per discussion on django-developers, a patch to remove the restrictions and update the documentation

Line 
1Index: trunk/docs/topics/db/sql.txt
2===================================================================
3--- trunk/docs/topics/db/sql.txt        (revision 15802)
4+++ trunk/docs/topics/db/sql.txt        (working copy)
5@@ -56,6 +56,13 @@
6     :attr:`~Options.db_table` option, which also lets you manually set the
7     database table name.
8 
9+.. admonition:: Statements permitted in ``.raw()`` queries
10+
11+    No checking is done on the SQL statement that is passed in to ``.raw()``.
12+    Django expects that the statement will return a set of rows from the
13+    database, but does nothing to enforce that. If the query does not
14+    return rows, a (possibly cryptic) error will result.
15+
16 Of course, this example isn't very exciting -- it's exactly the same as
17 running ``Person.objects.all()``. However, ``raw()`` has a bunch of other
18 options that make it very powerful.
19Index: trunk/django/db/models/sql/query.py
20===================================================================
21--- trunk/django/db/models/sql/query.py (revision 15802)
22+++ trunk/django/db/models/sql/query.py (working copy)
23@@ -31,7 +31,6 @@
24     """
25 
26     def __init__(self, sql, using, params=None):
27-        self.validate_sql(sql)
28         self.params = params or ()
29         self.sql = sql
30         self.using = using
31@@ -62,11 +61,6 @@
32         return [converter(column_meta[0])
33                 for column_meta in self.cursor.description]
34 
35-    def validate_sql(self, sql):
36-        if not sql.lower().strip().startswith('select'):
37-            raise InvalidQuery('Raw queries are limited to SELECT queries. Use '
38-                               'connection.cursor directly for other types of queries.')
39-
40     def __iter__(self):
41         # Always execute a new query for a new iterator.
42         # This could be optimized with a cache at the expense of RAM.