Excluding fields while using Values()

Mohit Jain Source

I have a model XYZ(name,age,sex), I am fetching all the fields using XYZ.objects.filter(name='ddd').values() this is returning all the fields in dict. How to exclude the field age?

I have tried the following, but it not working

XYZ.objects.filter(name='').exclude('sex').values()
pythondjangopython-3.x

Answers

answered 3 months ago Anup Yadav #1

Use this

XYZ.objects.filter(name='ddd').defer('age')      

OR

XYZ.objects.values('name','sex').filter(name='ddd')

OR 

fields = XYZ._meta.get_fields() # Try
fields.remove('age')
XYZ.object.filter(name='ddd').values(*fields)

OR

fields = list(XYZ._meta.get_fields())
fieldslist = [field.name for field in fields]  
XYZ.object.filter(name='ddd').values(*fieldslist)   

Put all list of fields into values exclude is not for selector it is for filter.

comments powered by Disqus