Opened 14 years ago

Closed 14 years ago

#14729 closed Bug (fixed)

RawQuerySet.__repr__ fails when params passed as list

Reported by: Marti Raudsepp Owned by: Alexey Smolsky
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords: raw query
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Django 1.2.3. The documentation suggests passing arguments to a Model.objects.raw() query as a list: http://docs.djangoproject.com/en/dev/topics/db/sql/#passing-parameters-into-raw

However, this breaks RawQuerySet.repr

In [1]: from django.contrib.auth import models

In [2]: r=models.User.objects.raw('select * from auth_user where id=%s', 10)

In [3]: repr(r)
Out[3]: "<RawQuerySet: 'select * from auth_user where id=10'>"

In [4]: r=models.User.objects.raw('select * from auth_user where id=%s and id=%s', (10, 10))

In [5]: repr(r)
Out[5]: "<RawQuerySet: 'select * from auth_user where id=10 and id=10'>"

In [6]: r=models.User.objects.raw('select * from auth_user where id=%s and id=%s', [10, 10])

In [7]: repr(r)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/tmp/foo/<ipython console> in <module>()

/usr/lib/pymodules/python2.6/django/db/models/query.pyc in __repr__(self)
   1374 
   1375     def __repr__(self):
-> 1376         return "<RawQuerySet: %r>" % (self.raw_query % self.params)
   1377 
   1378     def __getitem__(self, k):

TypeError: not enough arguments for format string

Attachments (1)

rev14757_raw_query_repr.diff (2.1 KB ) - added by Alexey Smolsky 14 years ago.
Tests moved in other place

Download all attachments as: .zip

Change History (7)

comment:1 by Matthias Kestenholz, 14 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:2 by Alexey Smolsky, 14 years ago

Has patch: set
Keywords: raw query added
milestone: 1.3
Needs tests: unset
Owner: changed from nobody to Alexey Smolsky
Status: newassigned

As official python documentation says(http://docs.python.org/library/stdtypes.html#string-formatting-operations), not-single argument for string formatting operation must be tuple or dictionary. So, we need pass list argument as tuple.

Patch with tests attached above.

comment:3 by Alexey Smolsky, 14 years ago

Version: 1.2SVN

by Alexey Smolsky, 14 years ago

Tests moved in other place

comment:4 by Matthias Kestenholz, 14 years ago

Triage Stage: AcceptedReady for checkin
Version: SVN1.2

Still applies correctly and fixes the reported problem.

Moving version back to 1.2 -- changing the version to SVN is not helpful because the field signifies since when a problem exists -- the fact that the ticket is still open already means that the problem still exists in SVN trunk.

Thanks!

comment:5 by James Addison, 14 years ago

milestone: 1.3
Severity: Normal
Type: Bug

comment:6 by Jannis Leidel, 14 years ago

Resolution: fixed
Status: assignedclosed

In [16088]:

Fixed #14729 -- RawQuerySet.repr fails when params passed as list. Thanks, intgr for ticket and accuser for patch.

Note: See TracTickets for help on using tickets.
Back to Top