On Sunday, 8 June 2014 13:24:01 UTC+1, Josh Smeaton wrote:
>
> I've thought about this previously, and what I had in mind was:
>
> - Expressions should support an ordering parameter of some kind in the 
> constructor, and save it to an ordering property. Default to ASC
> - If we go with Marc's suggestion, the __neg__ can set the above property. 
> This would look very consistent with the existing ordering api.
> - F() objects should parse their field argument, and internally set their 
> ordering attribute appropriately -> F('-title') should strip the `-` and 
> set a DESC ordering.
> - Since expressions can be nested, you can't return ASC/DESC from the 
> as_sql() method, otherwise you'd end up with weirdness like: Coalesce(field_a 
> ASC, field_b ASC) instead of Coalesce(field_a, field_b) ASC. Therefore, 
> the order_by machinery should call a `get_ordering()` (or something named 
> similarly) and generate the ASC/DESC token accordingly.
>

That sounds like a workable approach. If I'm understanding right, this 
means putting the ordering flag on the ExpressionNode class so that all 
nodes will have this whether or not they are to be used in an ordering 
context?

My only reservation about this when combined with __neg__ is that it means 
that certain mathematical expressions will behave oddly. For example, you 
might want to do something like

   Accounts.objects.filter(balance__lt=-F('overdraft_limit'))

but this will end up flipping the ordering rather than doing a mathematical 
negative. Is this dealt with by putting logic into the SQLEvaluator so that 
it knows whether it's evaluating in an ordering context or a SELECT or 
WHERE clause context and does the right thing?
 

> Usage:
>
> Article.objects.order_by(LowerCase('title', ordering='-'))
>
> Article.objects.order_by(-LowerCase('title')) # I prefer this form 
>
> Article.objects.order_by('-title') and 
> Article.objects.order_by(F('-title')) #  should be equivalent (a string 
> argument should be converted to an F())
>
>
> I'm unsure whether or not expressions should inspect their "children" for 
> an ordering if one isn't supplied in the outer-level:
>
> Article.objects.order_by(LowerCase('-title'))
>
>
> Should the Lowercase try to resolve the ordering of `title` and use it as 
> it's own? I don't think so, because it could lead to weird conflicts where 
> multiple arguments define different ordering:
>
> Article.objects.order_by(Coalesce('-title', '+description'))  # is ASC or 
> DESC used? Possibly confusing
>
>
> Thoughts? 
>


I think supporting this gives lots of potential problems without much in 
the way of gain. Even the "correct" form Coalesce('-title', '-description') 
just looks odd to me. The DESC modifier applies to the expression as a 
whole, not to any one field within it.

Tim

-- 
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/1560019a-47b6-4c90-85d6-8e8832a98510%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to