extra(), dict and list?

192 views
Skip to first unread message

Beau Hartshorne

unread,
Oct 10, 2006, 6:48:55 PM10/10/06
to django...@googlegroups.com
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))

Is this a known problem? Should I be writing my own SQL query instead
of trying to make extra work?

Thanks,
Beau

Russell Keith-Magee

unread,
Oct 10, 2006, 8:04:38 PM10/10/06
to django...@googlegroups.com
On 10/11/06, Beau Hartshorne <harts...@gmail.com> wrote:
>
> Is this a known problem? Should I be writing my own SQL query instead
> of trying to make extra work?

More a limitation than a problem. Python dictionaries are what they
are - your use case is a little unusual compared to what I have
usually seen the extra(select=...) field used for.

One workaround I can think of is to use
django.util.datatypes.SortedDict for your select clause. SortedDict is
a Django datatype that implements the dictionary protocol but
remembers insertion order. I haven't tried this myself, but I can't
think of any obvious reason that it wouldn't work.

The other alternative, as you suggested, is to hand-crank the SQL from
scratch. Not ideal, I know, but it would work :-)

Yours,
Russ Magee %-)

Malcolm Tredinnick

unread,
Oct 10, 2006, 8:30:06 PM10/10/06
to django...@googlegroups.com

Looks like you're right; there is going to be a problem there. The
"params" attribute is typically more useful with extra "where" clauses,
which is a list, so this doesn't come up in that case.

Could you file a ticket about this, please, so that we don't forget
about it forever.

Regards,
Malcolm


Beau Hartshorne

unread,
Oct 10, 2006, 9:19:52 PM10/10/06
to django...@googlegroups.com
On 10-Oct-06, at 5:04 PM, Russell Keith-Magee wrote:

> More a limitation than a problem. Python dictionaries are what they
> are - your use case is a little unusual compared to what I have
> usually seen the extra(select=...) field used for.
>
> One workaround I can think of is to use
> django.util.datatypes.SortedDict for your select clause. SortedDict is
> a Django datatype that implements the dictionary protocol but
> remembers insertion order. I haven't tried this myself, but I can't
> think of any obvious reason that it wouldn't work.
>
> The other alternative, as you suggested, is to hand-crank the SQL from
> scratch. Not ideal, I know, but it would work :-)

I tried using django.utils.datastructures.SortedDict, and it did not
solve the problem for me. I think the dict I pass to extra's select
parameter is merged with another dict, and so loses its order.

Thanks,
Beau

Beau Hartshorne

unread,
Oct 11, 2006, 4:41:06 PM10/11/06
to django...@googlegroups.com
On 10-Oct-06, at 5:30 PM, Malcolm Tredinnick wrote:

> Looks like you're right; there is going to be a problem there. The
> "params" attribute is typically more useful with extra "where"
> clauses,
> which is a list, so this doesn't come up in that case.
>
> Could you file a ticket about this, please, so that we don't forget
> about it forever.

Done: http://code.djangoproject.org/ticket/2902

Thanks!
Beau

Reply all
Reply to author
Forward
0 new messages