Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude field from values() or values_list()

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

like image 280
Azd325 Avatar asked May 31 '13 09:05

Azd325


2 Answers

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

Videos.objects.filter(...).defer('duration')
like image 121
FallenAngel Avatar answered Nov 15 '22 10:11

FallenAngel


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)
like image 18
iMom0 Avatar answered Nov 15 '22 08:11

iMom0