Officially (according to PEP-249) you are not supposed to be able to do 
that (pass a dictionary  of parameters), but PostgreSQL does it as an 
extension to the spec.  SQLite and ms-sql (both of which use format 
converters to send your queries out with '?' where you put the '%s') do NOT 
support it.  Shai is supplying a patch for Oracle (which uses a different 
format conversion) to support it.
 
There is a very noisy discussion taking place right now on the DB-SIG about 
the design of a future db-api version 3.  I am suggesting that we support 
automatic switching (similar to what you want to use here) but at the 
moment, I am loosing, and I do not expect that it will happen.  So you can 
do that now, on some databases, but do not expect it to be either universal 
or future proof.  I would advise sticking with a list of parameters for 
your present development efforts.

On Wednesday, May 22, 2013 3:37:55 AM UTC-6, roman wrote:
>
> Hello, 
>
>
> the docs say: 
>
> """ 
> Passing parameters into raw() 
>
> If you need to perform parameterized queries, you can use the params 
> argument to raw(): 
>
> >>> lname = 'Doe' 
> >>> Person.objects.raw('SELECT * FROM myapp_person WHERE last_name = %s', 
> [lname]) 
>
> params is a list of parameters. You’ll use %s placeholders in the 
> query string (regardless of your database engine); they’ll be replaced 
> with parameters from the params list. 
> """ 
>
> howerver this woks just fine and I see no reason why this should not be 
> done: 
>
> >>> param = dict(lname = 'Doe') 
> >>> qs = Person.objects.raw('SELECT * FROM myapp_person WHERE last_name = 
> %(lname)s', param) 
>
> however still this fails: 
>
> >>> repr(qs) 
> /home/user/vpy/dev/lib/python2.7/site-packages/django/db/models/query.pyc 
> in __repr__(self) 
>    1530 
>    1531     def __repr__(self): 
> -> 1532         return "<RawQuerySet: %r>" % (self.raw_query % 
> tuple(self.params)) 
>    1533 
>    1534     def __getitem__(self, k): 
>
> TypeError: format requires a mapping 
>
> If no one objects, I could write a patch to the code and the docs that 
> implements the functionality. 
>
> Regards Roman 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to