Bird
0
0

Which of the following is the correct way to safely filter users by username using Django ORM?

easy📝 Syntax Q3 of 15
Django - Security Best Practices
Which of the following is the correct way to safely filter users by username using Django ORM?
AUser.objects.filter(f"username = '{user_input}'")
BUser.objects.raw(f"SELECT * FROM users WHERE username = '{user_input}'")
CUser.objects.filter(username=user_input)
DUser.objects.raw("SELECT * FROM users")
Step-by-Step Solution
Solution:
  1. Step 1: Identify safe ORM filtering syntax

    filter() with keyword arguments safely handles user_input.
  2. Step 2: Analyze unsafe options

    Raw queries with string interpolation risk injection; filter() with string is invalid syntax.
  3. Final Answer:

    User.objects.filter(username=user_input) -> Option C
  4. Quick Check:

    Safe filtering syntax = User.objects.filter(username=user_input) [OK]
Quick Trick: Use keyword args in filter() for safety [OK]
Common Mistakes:
MISTAKES
  • Using raw SQL with string interpolation
  • Passing raw SQL string to filter()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes