从值()或values_list()中排除字段

时间:2022-03-17 01:40:59

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

有没有一种有效的方法可以从函数values()或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.

我知道我可以在结果中指定字段,但是如果我只需要一个字段就可以。就像在这种情况下,如果我有20个字段,如果我只想要一个不是。

Thanks

谢谢

2 个解决方案

#1


7  

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

您必须使用延迟这不会向您的选择查询添加已定义的字段。

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

#2


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)

#1


7  

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

您必须使用延迟这不会向您的选择查询添加已定义的字段。

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

#2


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)