Django Django REST Framework 1 — Questions and Answers
Question 1: What is Django REST Framework (DRF)?
- A powerful toolkit for building Web APIs on top of Django (Correct answer)
- Django's built-in JSON serialization library
- A database migration tool for REST-style schemas
- A frontend framework for Django projects
Correct answer: A powerful toolkit for building Web APIs on top of Django
DRF is a third-party package that extends Django with serializers, viewsets, authentication classes, and browsable API tooling for building RESTful APIs.
Question 2: What is a DRF Serializer used for?
- Converting model instances to Python native types (and back) for API input/output (Correct answer)
- Caching API responses
- Defining URL routes for API endpoints
- Handling authentication tokens
Correct answer: Converting model instances to Python native types (and back) for API input/output
Serializers handle data conversion between complex types like Django models and JSON-compatible Python primitives, including validation on input.
Question 3: Which DRF serializer automatically generates fields from a model?
- ModelSerializer (Correct answer)
- AutoSerializer
- SchemaSerializer
- FieldSerializer
Correct answer: ModelSerializer
ModelSerializer introspects a model to create serializer fields automatically, similar to how ModelForm works for forms.
Question 4: What is a DRF ViewSet?
- A class that combines CRUD logic for a resource into one class, used with Routers (Correct answer)
- A set of template views for API documentation
- A collection of URL patterns for an app
- A caching layer for API responses
Correct answer: A class that combines CRUD logic for a resource into one class, used with Routers
A ViewSet groups related views (list, create, retrieve, update, destroy) into one class and works with DRF's Router to auto-generate URL patterns.
Question 5: Which DRF authentication class validates API requests using a token in the Authorization header?
- TokenAuthentication (Correct answer)
- SessionAuthentication
- BasicAuthentication
- JWTAuthentication
Correct answer: TokenAuthentication
TokenAuthentication expects an 'Authorization: Token <key>' header and validates it against tokens stored in the authtoken_token database table.
Question 6: What is the purpose of DRF's `permission_classes` on a view?
- To restrict access to the API endpoint based on user authentication or permissions (Correct answer)
- To define which HTTP methods the endpoint accepts
- To set the response format (JSON/XML)
- To enable rate limiting on the endpoint
Correct answer: To restrict access to the API endpoint based on user authentication or permissions
permission_classes is a list of permission check classes that run before the view handler, returning 403 if any check fails.
What is Django REST Framework (DRF)?