2

I have the following error: Cannot resolve keyword 'attrName1' into field. Choices are: codesectrepmodel, configCons, id. My 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? I think the problem is that get_model returns the model class, not an object. Right?

1 Answer 1

5

When you are doing

.get(attrName1=attr1)

the attrName1 is actually keyword argument but not the variable. If you want to name fields dynamically you can try

.get(**{attrName1: attr1})
Sign up to request clarification or add additional context in comments.

2 Comments

well, I meant to replace attr1Instance=model1.objects.get(attrName1=attr1) with attr1Instance=model1.objects.get(**{attrName1: attr1})
well, this is just a standard syntax for passing keyword arguments as a dictionary in python docs.python.org/2/tutorial/controlflow.html#keyword-arguments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.