Opened 9 years ago

Closed 7 years ago

#2902 closed defect (fixed)

.extra()'s params list is incorrectly merged into the unordered select dictionary's %s slots.

Reported by: Beau Hartshorne <beau@…> Owned by: nobody
Component: Database layer (models, ORM) Version:
Severity: normal Keywords: qs-rf-fixed
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

.extra() expects a dictionary of extra select fields, and a list of query parameters to go with it. Since dictionaries are unordered, something like this fails:

.extra(select={'thingOne': 'REGEX %s', 'thingTwo': 'REGEX %s'}, params = (one, two))

I tried using a django.utils.datastructures.SortedDict, but that did not solve the problem.

More discussion: http://groups.google.com/group/django-users/browse_thread/thread/8ced145389650d62

Change History (10)

comment:1 Changed 9 years ago by Beau Hartshorne <beau@…>

  • Component changed from Admin interface to Database wrapper

comment:2 follow-up: Changed 8 years ago by Michael Radziej <mir@…>

I don't understand what you try to do with the fields. I cannot do a SELECT REGEX 'one' FROM tablename, so, what should the generated SQL look like for your example case? Perhaps I'm only too dumb here, happens a lot recently ;-)

Or did you mean:

.extra(select={'thingOne': 'fieldname REGEX %s', ...}, params=...)

?

comment:3 in reply to: ↑ 2 Changed 8 years ago by Beau Hartshorne <beau@…>

Replying to Michael Radziej <mir@noris.de>:

My SQL syntax is incorrect. You're right, this is what I meant:

.extra(select={'thingOne': 'fieldOne REGEX %s', 'thingTwo': 'fieldTwo REGEX %s'}, params = (one, two))

comment:4 Changed 8 years ago by Michael Radziej <mir@…>

  • Triage Stage changed from Unreviewed to Design decision needed

Thanks! As has been written in the mail thread, this is currently a limitation.

If you have any idea how to extend extra() so that you can use parameters within the SELECT dict, and still maintain compatibility to the old syntax, please add it to the ticket.

comment:5 Changed 8 years ago by Beau Hartshorne <beau@…>

Sure, just switch to django.utils.datastructures.SortedDict all the way through, and deprecate native dicts if there's more than one key. That seems to make the most sense in terms of API compatiblity, but I'm not sure if that will cause any performance problems or if it's difficult to implement.

Thoughts?

comment:6 Changed 8 years ago by mtredinnick

  • Keywords qs-rf added

comment:7 Changed 7 years ago by mtredinnick

(In [6603]) queryset-refactor: Added the ability to apply parameters to the select
fragments in QuerySet.extra(). Refs #2902

comment:8 Changed 7 years ago by mtredinnick

  • Keywords qs-rf-fixed added; qs-rf removed
  • Triage Stage changed from Design decision needed to Accepted

comment:9 Changed 7 years ago by mtredinnick

Note that [7340] changed the solution from [6603], in case anybody was tracking this.

comment:10 Changed 7 years ago by mtredinnick

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

(In [7477]) Merged the queryset-refactor branch into trunk.

This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.

Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658

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