Sachin
Sachin

Reputation: 3782

How to update value in the table referenced by foreign key in Django

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.

How can I effectively do the following two things

Upvotes: 0

Views: 636

Answers (1)

arie
arie

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

Related Questions