0

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()
shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90

1 Answers1

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.

Anup Yadav
  • 2,825
  • 3
  • 21
  • 30
  • Thank you but In my real model i have 48 attributes so is their any way i can do it without naming each one of them @anup yadav – Mohit Jain Feb 14 '18 at 10:31
  • use this `XYZ._meta.get_fields()` https://docs.djangoproject.com/en/1.11/ref/models/meta/ – Anup Yadav Feb 14 '18 at 10:45
  • How filters and ._meta you are trying together? This is just to get list of fields to add in select of Django ORM – Anup Yadav Feb 15 '18 at 11:46