Opened 5 years ago

Closed 4 years ago

#14729 closed Bug (fixed)

RawQuerySet.__repr__ fails when params passed as list

Reported by: intgr Owned by: accuser
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 accuser 5 years ago.
Tests moved in other place

Download all attachments as: .zip

Change History (7)

comment:1 Changed 5 years ago by mk

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 5 years ago by accuser

  • Has patch set
  • Keywords raw query added
  • milestone set to 1.3
  • Needs tests unset
  • Owner changed from nobody to accuser
  • Status changed from new to assigned

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 Changed 5 years ago by accuser

  • Version changed from 1.2 to SVN

Changed 5 years ago by accuser

Tests moved in other place

comment:4 Changed 4 years ago by mk

  • Triage Stage changed from Accepted to Ready for checkin
  • Version changed from SVN to 1.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 Changed 4 years ago by jaddison

  • milestone 1.3 deleted
  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 4 years ago by jezdez

  • Resolution set to fixed
  • Status changed from assigned to closed

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