Queryset/order_by option sort keys string manipulation

12 views
Skip to first unread message

Henry Versemann

unread,
Apr 16, 2015, 12:19:09 PM4/16/15
to django...@googlegroups.com
I have a string of sort keys which I've generated dynamically using the name of the table (model) that I'm trying to retrieve elements from, while putting them in a pre-determined sort order. The original sort key string looks like this:

    domain_code,request_url,request_description,id,request_method,response_type

Then I do some string manipulation of the "sort_keys" string object to produce this second version of the string:

    'domain_code','request_url','request_description','id','request_method','response_type'

to be used for the "sort_keys" of the Queryset "order_by" option.

I'm coding the Queryset with the "order_by" option like this:

    tblEntryLst = table_class.objects.order_by(sort_keys)

which fails with a django.core.exceptions.FieldError.
But if I code it the exact same way and hard-code the "sort_keys" like this:

    tblEntryLst = table_class.objects.order_by('domain_code','request_url','request_description','id','request_method','response_type')

Then everything works.

I'm thinking the reason it fails in the first instance is because the sort_keys object is maybe being evaluated before the Queryset command is actually run and so at that point the "sort_keys" are no longer in the proper comma-separated string object format to work within the confines of the command.

And in the second case the Queryset command does work because the sort-keys aren't evaluated before the command is actually run.

I hope this makes sense, and would appreciate any help in getting this to work without having to resort to hard-coding my sort keys, instead of using the dynamic ones.

Thanks for the help.

Henry


Reply all
Reply to author
Forward
0 new messages