python - Is it possible to use PK as FK when creating a related object NOT asking DB? -


i'm trying use code creating new m2m object

course_id = int(request.post['course'])     if not status.objects.get(course=course_id, user=request.user.profile, role="student"):         rel = status(course=course_id, user=request.user.profile,                      role="student")         rel.save() 

and result is

valueerror: cannot assign "1": "status.course" must "course" instance. 

i know possible use if stuff this:

course = course.objects.get(pk=request.post['course']) rel = status(course=course, user=request.user.profile,                  role="student") 

but in way ask course object db (after assign don't need this). overhead think.

it possible assign related object having pk, not asking object db?

you have problem here :

if not status.objects.get(**course=course_id**, user=request.user.profile, role="student"): 

status expecting couse instance , passing id. intead have do:

course_id = int(request.post['course']) if not status.objects.get(course__pk=course_instance, user=request.user.profile, role="student"):             rel = status(course=course_id, user=request.user.profile,                          role="student")             rel.save() 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -