Bird
0
0

Consider this async Django ORM code snippet:

medium📝 component behavior Q13 of 15
Django - Async Django
Consider this async Django ORM code snippet:
async def get_usernames():
    users = await User.objects.filter(is_active=True).values_list('username', flat=True)
    return list(users)

What will get_usernames() return?
AA list of usernames of active users
BA QuerySet of all users including inactive ones
CA list of all usernames regardless of active status
DAn error because <code>values_list</code> is not async
Step-by-Step Solution
Solution:
  1. Step 1: Understand the filter and values_list usage

    The filter limits users to those with is_active=True. The values_list with flat=True returns usernames as a list-like object.
  2. Step 2: Recognize async ORM behavior

    Using await fetches the filtered usernames asynchronously. Converting to list returns a list of usernames of active users.
  3. Final Answer:

    A list of usernames of active users -> Option A
  4. Quick Check:

    Filter + values_list + await = filtered usernames list [OK]
Quick Trick: Filter active users, await values_list, get list [OK]
Common Mistakes:
MISTAKES
  • Assuming values_list is synchronous and causes error
  • Ignoring the filter condition
  • Expecting a QuerySet instead of a list

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes