Divide queryset on 'read' / 'unread' in Django -
i'm trying create badge flag status 'read'/ 'unread' depending on users action (request).
models
class post(models.model): title = models.charfield(max_length=120) body = models.textfield() def get_absolute_url(self): return "/%s" %(self.id) ... class comment(models.model): post = models.foreignkey(post) ...
view
def list(request): qs_posts = post.object.all() ...
template
{% post in qs_posts %} <a href="{{ post.get_absolute_url }}" class="btn btn-primary">view</a> <p>{{ post.body }}</p> <p> messages: <span class="badge">{{ post.comment_set.count }}</span></p> <p> unread messages: <span class="badge red">{{ post.comment_set.count }}</span></p> {% endfor %}
i'm trying display count of 'unread'. don't need display specific user request.
so there few different ways solve particular problem based on given code. outline few of them ave options moving forward.
the first option simple helper method in post model. this:
class post(models.model): ... def get_unread_comment_count(): unread_comments = 0 comment in comment_set: unread_comments += 1 return unread_comments
then you'd call method in template so:
<p> unread messages: <span class="badge red">{{ post.get_unread_comment_count() }}</span></p>
another option put field in model itself.
class post(models.model): ... unread_count = models.integerfield(default=0)
this can bit costly each time new unread comment added (which frequent or not often, not sure of traffic site getting) have access database.
final option maybe designing own tag? i'm not familiar can on google , have crack @ if desire.
Comments
Post a Comment