Opened 8 years ago

Closed 8 years ago

#8218 closed (worksforme)

extra() with params and filter() does not return results

Reported by: valankar@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: extra filter params
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I'm seeing some strange behavior with the latest 1.0 alpha when using extra() with params and filter(). Here is an example based on the Polls app created in the tutorial.

>>> from mysite.polls.models import Poll, Choice
>>> p = Poll(question="What's up?", pub_date=datetime.datetime.now())
>>> p.save()
>>> polls = Poll.objects.extra(select={'blah': 'select count(*) from polls_poll where id=%s'}, params=[1])
>>> [p for p in polls]
[<Poll: Poll object>]
>>> [p.blah for p in polls]
[1]

Looks good so far, but as soon as I add a filter() to it that should return results, I get none:

>>> polls = Poll.objects.extra(select={'blah': 'select count(*) from polls_poll where id=%s'}, params=[1])
>>> polls = polls.filter(question="What's up?")
>>> [p for p in polls]
[]

Note that the filter by itself works fine:

>>> polls = Poll.objects.filter(question="What's up?")
>>> [p for p in polls]
[<Poll: Poll object>]

If I take out the extra() params it also works ok:

>>> polls = Poll.objects.extra(select={'blah': 'select count(*) from polls_poll where id=1'})
>>> polls = polls.filter(question="What's up?")
>>> [p for p in polls]
[<Poll: Poll object>]
>>> [p.blah for p in polls]
[1]

It's only when I combine with an extra() AND params that the problem shows up. I am using sqlite3. Thanks.

Change History (3)

comment:1 Changed 8 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Is this the same as #8191 ?

comment:2 Changed 8 years ago by mtredinnick

You're using the extra() method incorrectly. Passing parameters to an extra select should be done with the select_params parameter, not params.

If you fix your use of extra() does the problem go away? If not, what does the SQL look like for the query (you can view that by doing Poll.objects.extra(...).filter(...).query.as_sql())?

comment:3 Changed 8 years ago by valankar@…

  • Resolution set to worksforme
  • Status changed from new to closed

Indeed select_params works. Thanks!

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