我正在DjangoGirls教程上创建一个博客。这是我第一次使用Django,或多或少也是使用Python。
嗨,我遇到了一个问题。
无论我做什么,我的Post.objects.filter(published_date__isnull=False)命令仍然显示一个空列表。以下是命令和输出:
>>> post = Post.objects.get(id=1)
>>> post.publish()
>>> Post.objects.filter(published_date__isnull=False)
[]
>>> post = Post.objects.get(id=2)
>>> post.publish()
>>> Post.objects.filter(published_date__isnull=False)
[]
>>> first = Post.objects.get(id=1)
>>> first.publish()
>>> Post.objects.filter(published_date__isnull=False)
[]有人能帮我理解我做错了什么吗?我的数据库里有这么多帖子:
>>> Post.objects.all()
[<Post: Sample Title>, <Post: Sample-1 Title>, <Post: Sample-2 Title>, <Post: Sample-3 Title>, <Post: Sample-4 Title>, <Post: Sample-5 Title>, <Post: Sample-6 Title>]我在数据库里有很多作者:
>>> User.objects.all()
[<User: ola>, <User: dola>, <User: bula>, <User: kela>, <User: lala>]那我就不明白怎么回事了。当然,这个问题并没有影响到项目。我将继续学习本教程的其余部分,并且会进行得很好。谢谢。
发布于 2015-01-26 06:37:17
你没做错什么。命令Post.objects.filter(published_date__isnull=False)意味着如果有published_date不为空的帖子,那么返回空列表意味着所有的帖子都有空published_date。
编辑
重新启动shell,查看是否使用Post.objects.filter(published_date__isnull=False)获取任何数据
https://stackoverflow.com/questions/28145636
复制相似问题