It might be nice (although possibly impractical) to have something like
order_by(-LowerCase('title')) [using __neg__ on LowerCase]. That might put
logic on the "wrong" objects though. In any case, LowerCase('-title') looks
weird.

Naturally just passing in the string 'title' or '-title' should continue to
work as before.

I've not looked at the code at all but this seems like a nice design.

Marc


On 8 June 2014 11:28, Tim Martin <[email protected]> wrote:

> I've reworked my approach to ORDER BY to base it on Josh's patch, and it
> works very well. It's very straightforward to implement something like this:
>
> Article.objects.order_by(LowerCase('title'))  --> ORDER BY
> LOWER("some_articles"."title")
>
> This makes use of all the existing expression compiling logic, so there is
> no extra "ORDER BY" logic to worry about.
>
> What I'm not sure about is how to handle sort order. We can either support:
>
> Article.objects.order_by(LowerCase('-title'))
>
> which will require some special cases in the ORDER BY compilation logic
> (possibly quite substantial), or we can support:
>
> Article.objects.order_by(Desc(LowerCase('title'))
>
> The latter is a lot easier to implement, and strikes me as a nicer API.
> However, it's a significant step away from the ordering API we have at the
> moment. What do people think?
>
> Tim
>
>
> On Sunday, 1 June 2014 10:01:09 UTC+1, Tim Martin wrote:
>>
>> Funnily enough, I'd already seen that patch but I hadn't figured out the
>> full significance of it. I attempted to solve #22288 (
>> https://code.djangoproject.com/ticket/22288), but spotted that your
>> patch would possibly solve the problem entirely, and at the very least
>> breaks my attempt to solve that specific case.
>>
>> The expression concept seems like a good direction to be going in. I'd
>> definitely be interested in seeing how to extend this to solve the two
>> tickets I'm working on. I'll take a look at your code and see what I can do
>> with it, and ask if I get stuck.
>>
>> Tim
>>
>> On Sunday, 1 June 2014 09:46:02 UTC+1, Josh Smeaton wrote:
>>>
>>> I would like to point you towards a patch I'm currently working on which
>>> is essentially your option 2: https://github.com/django/django/pull/2496
>>>
>>> It doesn't seem very relevant at first, since it only applies to
>>> .annotate and .aggregate, but the infrastructure behind it certainly does.
>>> In short, the patch expands on the concept of "Expressions". Expressions
>>> are nodes in a tree that output SQL. Functions can be expressions, a field
>>> (F() object) can be an expression, or the number 5 can be an expression.
>>> Funnily enough, I implemented the SQL `Lower` function as a test:
>>> https://github.com/jarshwah/django/commit/362fec1330f1f40c45ece5769f9e35
>>> 047c87a010#diff-4c9f3e475cf7fa25e6d21c2fb7b8c30fR185
>>>
>>> What I'd like to do if this patch is accepted (and I believe it will be
>>> for 1.8; it's a matter of getting the backwards-compat right), is to expand
>>> the concept of expressions into the other pieces of the API, especially
>>> `order_by`. Once order_by understands expressions, it's trivial to order
>>> the result set by any kind of sql you could imagine..
>>>
>>> - .order_by(NullsLast('field'))
>>> - .order_by(Lower('field'))
>>> - .order_by(Coalesce(F('field'), F('otherfield')))
>>> etc.
>>>
>>> Marc is right that custom transforms could be used if order_by was
>>> taught the API. I would prefer to teach order_by about Expressions though,
>>> since F() supports custom lookups and transforms already. His version would
>>> then look like: .order_by(F('title__lower')). I can see the value in a
>>> keyword__based API that is similar to the rest of django (like .filter()),
>>> but I find the syntax limiting when trying to compose multiple expressions
>>> together.
>>>
>>> If you were interested in branching off and working on bringing the same
>>> functionality to order_by, I'd be happy to provide information or support
>>> if you had any questions.
>>>
>>> Regards,
>>>
>>> Josh
>>>
>>>  --
> You received this message because you are subscribed to the Google Groups
> "Django developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/a70e1fcb-665e-4421-9402-1621af559b11%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/a70e1fcb-665e-4421-9402-1621af559b11%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAMwjO1EHZT4ALZREOEPtnzSgymOy%2BNqDkXk%2Bzy5S4VVrupQU8A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to