This is a portion of a template tag code, where qs is a queryset.
def foo(qs):
...
context['key'] = qs.order_by('an_invalid_field_coming_from_user')
How can I check if the queryset will be ordered by a valid field before the code execution goes out of the scope of the template tag, other than forcing an evaluation?
The code as is does not raise an error since the queryset is not evaluated. qs.exists()
is not the answer either as it will execute the query without being ordered.
EDIT: Please note that the query may be more complex than my pre-edit simple example Foo.objects.all()
, eg, it may feature an extra()
method which results in joins.