Thomas Orozco
Thomas Orozco

Reputation: 55197

Django RawQuerySet.__repr__ : not enough arguments for format string

I'm using a RawQuerySet in Django, and I need to pass it a few parameters (5). I'm able to call the constructor using MyModel.objects.raw(SQL, params). The SQL is pretty long and not that relevant, but params is [991L, 991L, 991L, 7L, 3].

I do get a RawQuerySet in return. Hence, the call is OK.

However, my problem is that when __repr__ is called (through print here), I get a "Not enough arguments for format string" error. Now, what I don't get is the following:

  1. Why would I not have enough arguments since the call worked?
  2. [m.start() for m in re.finditer('%s', qs.raw_query)] gives me 5 items and so does qs.params.

I'm pretty sure I'm missing something, but I can't seem to find what.

Upvotes: 1

Views: 1266

Answers (1)

Thomas Orozco
Thomas Orozco

Reputation: 55197

I eventually found the explanation. I'm not sure this will be of much help to others, but anyways.

The problem came from the fact that params in the call was a list. It appears you can use a list to instantiate a RawQuerySet, but it is not converted into a tuple so you can't use it for string-formatting later on.

Conclusion I'll use MyModel.objects.raw(SQL, tuple(params)).

Upvotes: 2

Related Questions