Skip to content

Nested Comments

Jimmy Oty edited this page Mar 10, 2023 · 1 revision

Comments

The Comment model is used to create comments on a particular post. It has the following fields:

  • body: A text field to enter the comment text.
  • Post_id: A foreign key to the Stori model to which the comment belongs. This field is required.
  • user: A foreign key to the Account model which represents the user who created the comment. This field is required.
  • date_created: A datetime field that stores the date and time when the comment was created. This field is automatically generated by setting the auto_now_add attribute to True.
  • reactions: A generic relation to the Reaction model which allows users to react to the comment.
  • parent_comment: A foreign key to the Comment model which represents the parent comment of the current comment. This field is optional and can be left blank. If the current comment has a parent comment, then it is considered a child comment.
  • ordering: A meta option to order the comments based on the date they were created in descending order.

The Comment model also has two methods:

  • str(self): A method that returns a string representation of the comment by concatenating the comment body and the user's account name. child_comment(self): A method that returns all the child comments of the current comment. The Comment model has a property decorator:

  • is_parent: A property that returns a boolean value indicating whether the comment is a parent comment or not. If the comment has a parent comment, it is not considered a parent comment.

Clone this wiki locally