1👍
✅
Your way should be working, but it’s a little hard to manage. You should create a property method on your Topic
class and call it instead:
class Topic(models.Model):
# some fields go there
@property
def is_unread(self):
if self.last_posted > self.visitor.lasthit:
return True
else:
return False
Then when you do topic.is_unread
without the brackets it would return the value you want.
Python doc.
Edit:
Sounds like OP doesn’t have all parameters reside on Topic
model. In this case it falls back to the original implementation:
for topic in topics:
if topic.lastposted > request.user.lastvisit.thistopic:
topic.is_unread = True
else:
topic.is_unread = False
In your template you could do:
{% for topic in topics %}
{{ topic.is_unread }}
{% endfor %}
Source:stackexchange.com