Python / Django - * args as list

I use the method / function .order_by (); however, I want to dynamically build order_by fields. The problem is that .order_by () is waiting to receive a string or buffer. Thus, I cannot create a list or tuple or object to send a function to. How can I achieve this?

I wanted to do something like:

field_list = [] for field in fields: field_list.append( field ) model.objects.all().order_by( field_list ) 

???

+4
source share
1 answer

You can use model.objects.all().order_by(*field_list) ; this is because order_by accepts multiple string arguments, rather than multiple-string lists.

See This chapter in djangobook, find order_by and this for unpacking arguments.

+8
source

All Articles