Mona
Mona

Reputation: 1495

Cannnot Assign- must be a instance Django

I have a table called job_position and there is a foreign key called region.

position =job_position.objects.get(id=position_id)
position.department= department.objects.get(department_name =request.POST.get("department")),

the "department" is passed from the HTML form, and the value of it is "Asia"

I am quite sure the error is from the second line

Cannot assign "(<region: Asia>,)": "job_position.region" must be a "region" instance.

But when I am trying the code in python shell ,it works fine,ie.e can get the region object and pass it to the position object region attribute and works fine. But somehow it does not works in the web development. Anyone can help explain? Thanks very much

Upvotes: 0

Views: 893

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174622

Cannot assign "(<region: Asia>,)"

As you have a comma at the end of your line, it turned it object into a tuple. Simply remove the comma:

position.department = department.objects.get(department_name=request.POST.get("department"))

Upvotes: 2

Related Questions