Reputation: 17013
Sorry if I'm missing something obvious here as my search isn't turning up anything relevant. I am doing a Django database get query and would like to get each field name during a for loop so that I can do evaluations of it ( if fieldname = "blah") and so on, but I can't seem to figure this out, any advice is appreciated
db_get_data = Modelname.objects.all()
for cur_db_get_data in db_get_data:
#something to get the field name from cur_db_get_data
Upvotes: 2
Views: 1907
Reputation: 2573
Try the _meta.fields property.
db_get_data = Model.objects.all()
for cur in db_get_data:
for field in cur._meta.fields: # field is a django field
if field.name == 'id':
print 'found primary key'
Upvotes: 4