--
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 django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
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/141c1486-ca35-497d-82f7-13ad83900b9d%40googlegroups.com.
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 django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
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.
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())
Article.objects.order_by(LowerCase('-title'))
Article.objects.order_by(Coalesce('-title', '+description')) # is ASC or DESC used? Possibly confusing
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.
Usage:Article.objects.order_by(LowerCase('title', ordering='-'))Article.objects.order_by(-LowerCase('title')) # I prefer this formArticle.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 confusingThoughts?
--
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 django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
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.
.order_by( F('field_a')+F('field_b') )
--
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 django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
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/6365dba1-2dfc-469d-988b-3cb800cc0f76%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAMwjO1E36SeDW4R_jQOcw%3DkGV%2BwG5uaE%3D1Vif5DyDgwJY3ikcA%40mail.gmail.com.
Model.objects.all().order_by( F('field')+F('other') )
Model.objects.all().order_by( -Wrap(F('field')+F('other')) )
Model.objects.annotate( Wrap(F('field')+5, output_type=IntegerField()) )
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/4e8e8101-87d2-4d4a-8c1c-79f885fe5291%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/4e8e8101-87d2-4d4a-8c1c-79f885fe5291%40googlegroups.com.
--
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 django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
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/20cd8bd0-9d5d-4ed3-9748-1412188e6726%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CALuYSZVYnXHapfvf5SwPJxLYkxiv%3DTPyJg2XYGFVhCPSyc7Fgw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAGdCwBuHBtQLsVEeftfZAVrx6hdZSvz9ED%3DMmjZvyg%2B--%3D6moQ%40mail.gmail.com.
I'll go ahead and try to implement this using __neg__() to invert the ordering.
--
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 django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
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/3f909f23-00a5-4bec-a21c-1333cec912b9%40googlegroups.com.
- should expressions in order_by support random ordering (?) ? I don't think so, but I haven't ever had a need for random ordering so I'm not sure.
> I haven't yet implemented the LowerCase and related functions
I think we can build out a handy little library of utility functions like Lower and Coalesce that could(should) be included directly in django. But it'll also open up, to library developers, the ability to easily develop backend specific structures, similar to parts of the django.contrib.postgres kickstarter.
> However, I think having some special case code in filter(), annotate() and anything else that takes expressions would be OKI strongly disagree with this. Expressions are context insensitive at the moment, which means they can be used anywhere. If we start externalising behaviour based on the context of use, it'll lead us to some very confusing code and a tonne of special cases.
It would be slightly better if you could check the context from the `prepare()` method (are we preparing order_by or not),
but then I think we'll run into issues where the prepare breaks for 3rd party backends - by checking private data.
- should expressions in order_by support random ordering (?) ? I don't think so, but I haven't ever had a need for random ordering so I'm not sure.
- could we reduce the complexity of the order_by machinery by forcing all arguments (internally) to be expressions? `order_by('field_a') -> order_by(F('field_a'))` etc
def lower_mssql(self, compiler, connection):self.function = 'ToLower'super(Lower, self).as_sql(compiler, connection)Lower.as_mssql = lower_mssql
def prepare(self, query, ..):if query.ordering is None and self.has_ordering():# boom
> Model.objects.filter(...).order_by( F('fld_a').desc(), F('fld_b')).asc() )
> Model.objects.filter(...).order_by( (F('fld_a')+F('fld_b')).desc() )I actually really like this. It's simple, clear, and gets around the issues being discussed. It still requires Expressions to know about ordering (to implement the asc/desc methods) but I think that's fine.The other problem with having ordering objects being of a different type means that you would have to call asc or desc. But now I think of it, the order_by method could just call asc, by default, on any expressions passed in.
Nice work, I think that's a lot better. The only thing that now worries me is that order_by doesn't accept sql parameters for some reason. I'm not sure how many expressions produce sql_params and if they'd be useful in an ordering context. It might be worth looking into that a little more.
Perhaps we can start another discussion on the ML so we can address these concerns without it being hidden inside this thread?