JSON From a View
The smallest possible API. A view that returns JsonResponse instead of render. No extra library, and perfectly reasonable for a handful of endpoints.
.values("id", "title") returns dictionaries rather than model instances, which serialize directly.
Return an object, not a bare array. {"results": [...]} rather than [...].
A bare array leaves you nowhere to put a count, a next page, or an error, and adding one later breaks every client that already parses it. The wrapper costs nothing today and buys room to change.
Choose the fields deliberately. Serializing a whole model is how email addresses and password hashes end up in public responses. Name what goes out, exactly as you name form fields, and for the same reason.
Mark this lesson complete
Signed in, your progress follows you to every device.