An error occurs when you try to access the Model Manager through an instance of the model. You have used lowercase class names. This makes it difficult to state whether the error is caused by an instance accessing Manager or not. Since other scenarios that may cause this error are unknown, I assume that you have somehow mixed the topic variable so that you end up pointing to an instance of the topic model instead of the class.
This line is the culprit:
forum.topic_count = topic.objects.filter(forum = forum).count() # ^^^^^
You should use:
forum.topic_count = Topic.objects.filter(forum = forum).count() # ^^^^^ # Model, not instance.
What is going wrong? objects is a Manager available at the class level, not instances. See the documentation for obtaining objects for more details. Money quote:
Managers are only available through model classes, not from model instances, to ensure separation between table-level operations and record-level operations.
(emphasis added)
Update
See comments from @ Daniel below. It is a good idea (no, you MUST: P) use a header for class names. For example topic instead of topic . Class names cause some confusion as to whether you are referencing an instance or class. Since Manager isn't accessible via <model> instances very specific, I can offer a solution. The error may not always be obvious.
Manoj Govindan Oct 06 2018-10-06 16:07
source share