Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChristopher Gunawan
10,754 PointsI don't understand what is going on quiz.course = course. Help please...
@login_required
def quiz_create(request, course_pk):
course = get_object_or_404(models.Course, pk=course_pk)
form = forms.QuizForm()
if request.method == 'POST':
form = forms.QuizForm(request.POST)
if form.is_valid():
quiz = form.save(commit=False)
quiz.course = course
quiz.save()
messages.add_message(request, messages.SUCCESS, "Quiz added!")
return HttpResponseRedirect(quiz.get_absolute_url())
return render(request, 'courses/quiz_form.html', {'form': form})
What is exactly going on with quiz.course = course? Thanks in advance!
1 Answer
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsHi there, I presume quiz has a relation to course via a foreignkey in the model. The code
quiz.course = course
assigns that object to the Quiz model related field course
Christopher Gunawan
10,754 PointsChristopher Gunawan
10,754 PointsThank you Stuart, you're a champ.