Exclude field from values() or values_list()

跟風遠走 提交于 2019-12-14 01:23:14

问题


Is there an efficient way to exclude fields from the function values() or values_list.

e.g

Videos.objects.filter(id=1).get().values()

I want to exclude from this queryset the field duration.

I know that I can specify fields what I want to have in the result but what if I want everything but only one field not. Like in the cases if I have 20 fields and if I want only one from them not.

Thanks


回答1:


You must use defer This will not add defined fields to your select query.

Videos.objects.filter(...).defer('duration')



回答2:


You can get all fields first, then pop out the fields you do not want:

fields = Video._meta.get_all_field_names()
fields.remove('id')
Video.object.filter(...).values(*fields)


来源:https://stackoverflow.com/questions/16854659/exclude-field-from-values-or-values-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!