Q1
In Django ORM, why does len(User.objects.all()) burn memory compared to User.objects.count()?
len(qs) evaluates the entire QuerySet, fetching every row and instantiating Python model objects in RAM just to count them. .count() executes a lightweight SELECT COUNT(*) directly in MySQL/PostgreSQL.
Q2
How do select_related and prefetch_related eliminate Django N+1 query disasters differently?
select_related performs a SQL INNER/LEFT JOIN in one query for ForeignKey / OneToOneField. prefetch_related issues a second WHERE id IN (...) query and joins in Python for ManyToMany and reverse FK relations.
Q3
Why can Django F() expressions prevent race conditions during concurrent counter increments (views += 1)?
Standard obj.views += 1; obj.save() reads stale values into Python and overwrites concurrent updates. F('views') + 1 pushes atomic arithmetic (SET views = views + 1) directly inside SQL.
Q4
What breaks when forgetting transaction.atomic() around multi-step Django REST Framework payments or orders?
Without @transaction.atomic, if step 2 fails after step 1 saved an order row, Django commits step 1 automatically (autocommit is on by default), leaving corrupted half-created database records.
Q5
Why should DRF ModelViewSet validate query params and permissions per object (has_object_permission) rather than only IsAuthenticated?
IsAuthenticated proves *who* you are, not *what* you own. Without object-level checks or queryset filtering (queryset.filter(owner=request.user)), any logged-in user can read or delete another user's ID (IDOR vulnerability).
Q6
What happens if Django DEBUG = True stays enabled in production?
Any unhandled 500 error prints local variables, settings keys, SQL queries, and file paths publicly in the browser, while Django also caches every SQL query in RAM until the worker leaks out of memory.
Q7
In React + Django REST Framework, why does Axios withCredentials: true require specific CORS origins instead of *?
Browsers reject Access-Control-Allow-Origin: * whenever Allow-Credentials: true is present. Specify exact frontend domains inside CORS_ALLOWED_ORIGINS alongside CSRF trusted origins.
Q8
Why is Django get_or_create() still prone to IntegrityError under simultaneous duplicate API requests?
Two concurrent requests can both see no matching row, then both run INSERT, colliding on the UNIQUE index. Wrap or catch IntegrityError cleanly with a retry lookup.
Q9
How do DRF Serializer validate_<field> vs validate() methods differ in execution order?
Field-level validate_email() runs per field first (only if type validation passes), while object-level validate(attrs) runs afterward for cross-field rules like password === confirm_password.
Q10
Why does serving Django media uploads via static() fail once DEBUG = False with Gunicorn?
Django refuses to serve static/media routes efficiently in production. Always route /static/ (collected via collectstatic) and /media/ through Nginx, WhiteNoise, or S3 storage.