I have comment model that references (foreign key) a post model and there is a boolean field in the post model that tells whether this particular post has any comment or not.
-
What I want to do is to update this boolean field of the post whenever a comment is made on that post, for this what do I have to do in the view.
-
Also when I show all the posts on a page, I want to show those first which do not have any comment, so that people can comment on them.
How can I effectively do the following two things
You could use the
comment_was_posted signalto update the associatedPostonce there is a new comment. See this thread to get the general idea: Django notification on comment submissionAlthough it might be better in this case to create a
count_comments()method on yourPostmodel.To order and filter your Posts by the number of Comments for a given
Postcheck Django’s docs on aggregation or this blog post: http://agiliq.com/blog/2009/08/django-aggregation-tutorial/