[英]Python/Django - *args as list
I'm using the .order_by() method/function; 我正在使用.order_by()方法/函数; however, I want to construct the order_by fields dynamically.
但是,我想动态构造order_by字段。 The problem is .order_by() expects to receive a string or buffer.
问题是.order_by()期望接收字符串或缓冲区。 So, I can't build a list or tuple or object to send to the function.
所以,我无法构建一个列表或元组或对象来发送到该函数。 How can I achieve this goal?
我怎样才能实现这个目标?
I wanted to do something like: 我想做的事情如下:
field_list = []
for field in fields:
field_list.append( field )
model.objects.all().order_by( field_list )
??? ???
You can use model.objects.all().order_by(*field_list)
; 你可以使用
model.objects.all().order_by(*field_list)
; this is due to the fact that order_by
accepts multiple string arguments, not lists of multiple strings. 这是因为
order_by
接受多个字符串参数,而不是多个字符串的列表。
See This chapter in djangobook, search for order_by
, and this for arguments unpacking. 请参见本章中djangobook,搜索
order_by
,和这对参数拆包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.