Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save exit99/934a21ddea24a6f06088dd4f5cb14208 to your computer and use it in GitHub Desktop.
Save exit99/934a21ddea24a6f06088dd4f5cb14208 to your computer and use it in GitHub Desktop.
# This will return None if not found, as opposed to raising an exception
classroom = Classroom.objects.filter(pk=pk).first()
from django.shortcuts import get_object_or_404
# This will return a HTTP 404 response if the objects does not exist
classroom = get_object_or_404(Classroom, pk=pk)
try:
classroom = Classroom.objects.get(pk=pk)
except Classroom.DoesNotExist:
classroom = None
questions = Question.objects.filter(classroom=classroom).order_by('-created')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment