简体   繁体   中英

Django query passing parameters into values()

I have a Django query like this and I want to be able to pass the "values" param from a variable. Reason for this I can standardize all my list values otherwise it will be hardcoded on every values.

So for example, this works:

employee_list = Employee.objects.all().values("id", "username").order_by('id') 

But I cannot do this. It doesnt work:

val_params = ["id", "username"]
employee_list = Employee.objects.all().values(val_params).order_by('id') 

what kind of val_params can i use to pass into values() ? Is there a way to do this ?

你可以做

employee_list = Employee.objects.all().values(*val_params).order_by('id')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM