Writing the Comment Model and Serializer
Explore the process of building a comment feature in a Django social media app. Learn to define Comment models with relationships to users and posts, create comments in the Django shell, and write serializers that validate and manage comment data, ensuring only authenticated users can interact with comments as required.
A comment in the context of this project will represent short text that can be viewed by anyone but only be created or updated by authenticated users.
Feature requirements
Here’s what the requirements for this feature look like:
Any user can read comments.
Authenticated users can create comments under posts.
The comment author and post author can delete comments.
The comment author can update posts.
The Comment table
Looking at these requirements, we can definitely start with writing the model first. But first, let’s quickly talk about the structure of the Comment table in the database:
The Comment table structure
|
|
A comment will mostly have ...