Frameworks, ORM, validation, auth and deployment
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freeDjango is a full-stack framework with built-in ORM, authentication, and admin, while Flask is a lightweight microframework that gives you more flexibility but requires you to add extensions for things like database access.
FastAPI uses Pydantic models and Python type hints to automatically validate and parse incoming request data, raising clear errors if the data doesn't match the expected schema.
Migrations in Django track and apply changes to the database schema, ensuring that the database stays in sync with the models defined in code.
You use the @app.route() decorator to map a URL path to a Python function, which handles the request and returns a response.
I'd choose FastAPI when I need high-performance async APIs, automatic OpenAPI docs, or tight control over validation and serialization. Django is better for projects needing a full-featured admin, built-in authentication, and rapid prototyping of complex apps.
FastAPI lets you declare dependencies using the Depends function, which can inject things like database sessions or authentication checks into your endpoint functions. This makes code more modular and testable, since dependencies are explicit and easy to override in tests.
The N+1 problem happens when an ORM issues one query for a list of objects and then an additional query for each object's related data. In Django, I use select_related for foreign keys and prefetch_related for many-to-many or reverse relationships to fetch related data in fewer queries.
Django's admin auto-generates CRUD interfaces for registered models, letting you manage database records through a web UI. You can customize the admin by registering models and configuring their display and permissions.
Session authentication stores user state on the server and uses a cookie to track the session. JWT is stateless; the server issues a signed token that the client sends with each request, and the server validates the token without storing session data.
I run Django behind a WSGI server like Gunicorn, often inside a Docker container, and serve static files with a reverse proxy like Nginx. I also set environment variables for settings like DEBUG and database credentials.
I use FastAPI's TestClient to simulate HTTP requests in tests. With pytest, I often define a fixture that returns a TestClient instance, then write tests that call endpoints and assert on the responses.
Middleware in Django are hooks that process requests and responses globally. They're used for things like authentication, logging, and modifying requests or responses before they reach the view or after the response is generated.