python - django: get model instance with dynamic variable name and value -
i have following error: cannot resolve keyword 'attrname1' field. choices are: codesectrepmodel, configcons, id
. code:
modelname1="configconsmodel" model1 = get_model('actsinformationretrieval', modelname1) print "model attr", model1._meta.get_all_field_names() #displays ['codesectrepmodel', 'configcons', 'id'] print "attrname1", attrname1 #displays configcons print "attr1", attr1 #displays ecofin attr1instance=model1.objects.get(attrname1=attr1)
what's wrong? think problem get_model
returns model class, not object. right?
when doing
.get(attrname1=attr1)
the attrname1
keyword argument not variable. if want name fields dynamically can try
.get(**{attrname1: attr1})
Comments
Post a Comment