Reputation: 3782
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
Upvotes: 0
Views: 636
Reputation: 18972
You could use the comment_was_posted signal
to update the associated Post
once there is a new comment. See this thread to get the general idea: Django notification on comment submission
Although it might be better in this case to create a count_comments()
method on your Post
model.
To order and filter your Posts by the number of Comments for a given Post
check Django's docs on aggregation or this blog post: http://agiliq.com/blog/2009/08/django-aggregation-tutorial/
Upvotes: 1