Problem statement
In this Django project, rebuild the newsroom's two article endpoints on DRF generic views. Reimplement ArticleListCreateView and ArticleDetailView in app/views.py so each method follows the generic contract and the author of a new article is decided by the server. Today both classes are hand-written: a create answers 200 instead of 201, a delete answers 200 with a chatty body instead of an empty 204, a PUT quietly accepts a half-filled body, PATCH was never written at all, an unknown id raises instead of answering 404, and the author is whatever account the client claimed in the payload.
Requirements
Implement:
- ArticleListCreateView and ArticleDetailView in app/views.py
The following files are already implemented and provided: app/models.py, app/serializers.py, app/urls.py and project/settings.py. Article holds title (CharField(max_length=120)), body (TextField, required), author (a required ForeignKey to the user model) and created_at (auto_now_add). ArticleSerializer is a finished ModelSerializer over Article carrying exactly id, title, body, author and created_at, with id, author and created_at read-only, so a value a client posts for author never reaches validated_data and the serializer will not fill it in: whoever saves the object has to supply it. app/urls.py routes / to home, /api/articles/ to ArticleListCreateView under the name article-list, and /api/articles/<int:pk>/ to ArticleDetailView under the name article-detail; both class names therefore stay as they are. home is the given landing page answering the running server's port and is no part of this exercise. project/settings.py already sets the API-wide policy - reading is open to anyone, writing requires a signed-in account - so nothing in app/views.py repeats it.
1. The collection
- A GET at /api/articles/ returns 200 with every article serialized, as a plain list, each row carrying exactly the keys id, title, body, author and created_at.
- A POST at /api/articles/ returns 201, stores the article, and returns that same key set in the body.
- A POST missing a required field returns 400 with body named among the response keys, and stores nothing.
2. The single row
- A GET at /api/articles/<pk>/ returns 200 with that one article under the same five keys.
- A PUT and a PATCH each return 200 with the updated article.
- A DELETE returns 204 with an empty body, removes that row, and leaves every other row in place.
3. PUT replaces, PATCH amends
- A PUT carrying only title returns 400 with body named in the response, and the stored article is unchanged in both fields.
- The same body sent as a PATCH returns 200, changes title, and leaves the stored body exactly as it was.
- A PATCH carrying only body changes body and leaves title and author as they were.
- Do not make PUT behave like PATCH. The two differ only in the partial flag the generic update already sets per method.
4. An id nobody has
- GET, PUT, PATCH and DELETE on an id that is not in the view's queryset all return 404.
- Nothing is deleted on the way past: both seeded rows are still there after a DELETE that answered 404.
5. The author is the request user
- A POST that carries no author stores the requesting account as the author, and the response reports that account's id under author.
- A POST that carries another user's id stores the requesting account anyway: the rest of the body is used and the author the client picked is not.
- The author never comes out of the request body, not even as a fallback when the body happens to carry one.
Notes
- A generic view asks for two attributes and supplies the rest: queryset, the rows the view works over, and serializer_class, the shape they take going out and coming in.
- generics.ListCreateAPIView carries get and post. generics.RetrieveUpdateDestroyAPIView carries get, put, patch and delete over the single row its URL names. Those two, or the mixins they are composed from, are the whole answer: no ViewSet and no router.
- 404 is the queryset talking. The generic view looks the row up in queryset and answers 404 when it is not there, on retrieve, update and destroy alike, so no method needs a missing-row branch of its own.
- perform_create is the hook the generic create calls instead of serializer.save(), and keyword arguments passed to serializer.save() are written onto the object alongside the validated body. self.request.user is the account the request arrived as, so a server-managed value gets set without ever being accepted from a client.
- Do not hand-write the fetch-validate-save-respond cycle again, and do not override update or destroy: the generic versions are already correct.
- Only app/views.py changes. Leave the home view where it is, and do not add pagination, filtering or throttling - the list response is read as a plain list of rows.
