【发布时间】:2013-01-08 06:03:30
【问题描述】:
我有一个由 django 构建的博客应用程序,如果有新评论,我想通知博主,所以这就是我所做的
class Blog(models.Model):
lastview = models.DateTimeField('self last view date')
class Comment(models.Model):
blog = models.ForeignKey(Blog)
timestamp = models.DateTimeField('comment date')
user_blog_list = Blog.Objects.filter(author = request.user)
user_blog_com = {}
for blog in user_blog_list:
user_blog_com [blog] =list(Comment.objects.filter(blog = blog ))
现在user_blog_com 是dict 的样子
{
(Blog: blogname1):[(Comment:comment1),(Comment:comment2)],
(Blog: blogname2):[(Comment:comment1),(Comment:comment2)],
}
接下来我需要将每个评论的时间戳与博客的 lastview 进行比较,以确定该评论是否被博主查看,但我不知道如何。
我想要的是一张类似的光盘
{
(Blog: blogname):[(Comment:unviewed_comment),(Comment:unviewed_comment)],
}
请帮忙!!!
我试试这个
user_blog_com = {}
for blog in user_blog_list:
user_blog_com [blog] =list(Comment.objects.filter(blog = blog ,timestamp > blog.lastview ))
get an error: non-keyword arg after keyword arg
【问题讨论】:
-
在查询
Comments 时为什么不这样做? -
dicts 没有顺序...你可能想看看ordereddict
-
你的意思是 user_blog_com [blog] =list(Comment.objects.filter(blog = blog, timestamp > blog.lastview )) ?
-
@chenliang 我相信你是从错误的方向来解决这个问题的。您正在执行许多可以通过单个 ORM 查询来实现的迂回处理。
-
是的,我做到了,我得到它的工作方式:user_blog_com [blog] =list(Comment.objects.filter(blog = blog ,timestamp__ge = blog.lastview))
标签: python django list dictionary