2

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
Rick
  • 16,612
  • 34
  • 110
  • 163
  • 1
    This might help answer your question. http://stackoverflow.com/questions/2384436/how-to-introspect-django-model-fields – Tao Sep 02 '10 at 20:47

1 Answers1

4

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'
OmerGertel
  • 2,573
  • 1
  • 19
  • 27