Python | Django QuerySets

Last Updated :
Discuss
Comments

Question 1

What happens when get() is called on a queryset with no matching records, and how is this different from calling filter() when exactly one record matches?

  • get() returns an empty list and filter() raises DoesNotExist

  • get() raises a DoesNotExist error and filter() returns a queryset containing the single matching object

  • get() returns None and filter() raises MultipleObjectsReturned

  • get() runs raw SQL and filter() uses the ORM

Question 2

Which query returns posts published after a given date while keeping only records with status set to published, and which operator filters values greater than the date?

  • Post.objects.filter(status='published', published__gt=date) which uses gt for greater-than

  • Post.objects.exclude(status='draft').filter(published__lt=date) which uses lt for less-than

  • Post.objects.get(status='published', published__lte=date) which uses lte for less-than-or-equal

  • Post.objects.all().exclude(published__gte=date) which uses gte for greater-than-or-equal

Question 3

For performing an OR-based search such as finding posts where the title contains Django or the author is John, which import and filter syntax are correct?

  • from django.db import Q and Post.objects.filter(title__icontains='Django' | Q(author='John'))

  • from django import Q and Post.objects.exclude(~Q(title__icontains='Django') & Q(author='John'))

  • from models import Q and Post.objects.get(title__icontains='Django') or Q(author='John')

  • from django.db import Q and Post.objects.filter(Q(title__icontains='Django') | Q(author='John'))

Question 4

When using order_by('created') on a comments queryset, what is the default sort direction and how can this relate to query efficiency?

  • Ascending and always causes a full table scan

  • Descending and requires reverse() for ascending

  • Ascending which shows oldest first and can use an index on the field for efficient sorting

  • Random with no index impact

Question 5

When sorting products by price in descending order and then by name in ascending order for consistent tie handling, which order_by call is correct?

  • Product.objects.order_by('-price', 'name') which applies name as the secondary sort

  • Product.objects.order_by('name', '-price') which prioritizes name instead of price

  • Product.objects.order_by('price').reverse('name') which is not valid usage

  • Product.objects.order_by('-price' + 'name') which treats the fields as a single string

Question 6

For a view that first orders posts by date in ascending order, which method reverses that ordering without rewriting the order_by call, and why is using -date directly often better for large datasets?

  • Post.objects.filter(published=True).reverse('date') which treats the argument as a lookup and is invalid

  • Post.objects.order_by('date').exclude(reverse=True) which does nothing related to reversing

  • Post.objects.reverse().order_by('date') which reverses before ordering and produces inefficient, unpredictable results

  • Post.objects.order_by('date').reverse() which flips the order and using -date directly is faster because the database can sort once without an extra reversal step

Tags:

There are 6 questions to complete.

Take a part in the ongoing discussion