Question bank

Pick an answer to see if you're right — with an explanation.

DjangoMedium
Why should `success_url` on a class-based view use `reverse_lazy`?
areverse() is deprecated
bIt caches the URL for faster redirects
cIt works with POST only
dThe URLconf isn't loaded when the class attribute is evaluated at import time
DjangoHard
How many database queries does this run?
qs = Post.objects.filter(published=True)
qs = qs.order_by('-created')
for p in qs[:5]:
    print(p.title)
aOne, when the loop starts iterating
bTwo, one per queryset line
cThree
dSix, one per post plus one
DjangoHard
How many queries does this loop run for 50 orders (no other optimisation)?
for o in Order.objects.all():
    print(len(o.items.all()))
a1
b2
c51
d100
DjangoHard
When should a project switch to a custom user model?
aOnly after real users exist in the database
bAt the start, before the first migration, by setting AUTH_USER_MODEL
cNever; Django forbids it
dAny time, it is a one-line change with no data impact
DjangoHard
Which statement decrements stock safely even if two requests run at the same moment?
ap.stock = p.stock - 1; p.save()
bProduct.objects.filter(pk=p.pk).update(stock=F('stock') - 1)
cp.refresh_from_db(); p.stock -= 1; p.save()
dProduct.objects.get(pk=p.pk).stock -= 1