将字段列表传递给QuerySet.values() [英] Pass list of fields to QuerySet.values( )
本文介绍了将字段列表传递给QuerySet.values()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以传递要由 QuerySet.values().我有一个模型,我想在不同情况下从中检索不同的字段集.
Is there a way I can pass a list of fields to be retrieved by the QuerySet.values(). I have a model and I want to retrieve different sets of fields from it on different occasions.
推荐答案
传递给函数时,可以使用 *
运算符将列表扩展为单独的参数,如此处.
You can use the *
operator to expand a list out into separate arguments when passed to a function, as described here in the Python tutorial.
>>> qs = User.objects.all()
>>> values = ['first_name', 'email']
>>> qs.values(*values)
收益
[{'first_name': u'aaaa', 'email': u'a@b.com'},
{'first_name': u'', 'email': u'abc@def.com'},
{'first_name': u'', 'email': u'abcd@gmail.com'},
'...(remaining elements truncated)...']
(为简洁起见,我进一步将输出截断了.)
(I further truncated the output for brevity).
这篇关于将字段列表传递给QuerySet.values()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文