Django Mongodb无法通过objects.all找到对象。

时间:2015-12-16 17:18:46

标签: python django mongodb shell

我从一开始就遵循教程here,直到(包括)“添加一些模板”部分。这是我的代码:

models.py

> db.blogs_post.find().pretty()
{
    "_id" : ObjectId("56716f162b464a2fd3f252f8"),
    "tags" : [
        "mongodb",
        "django"
    ],
    "text" : "Just wanted to drop a note",
    "title" : "Hello MongoDB",
    "comments" : [
        "Great post!",
        "Please, do more of these!"
    ]
}
{
    "_id" : ObjectId("56718f752b464a35593d7f23"),
    "text" : "People from haven also blog. This is a sample blog post from haven",
    "tags" : [
        "haven",
        "blog",
        "quant"
    ],
    "comments" : [
        {
            "text" : "This is a test comment",
            "created_on" : ISODate("2015-12-16T16:17:22.912Z"),
            "id" : ObjectId("56718e922b464a35593d7f22"),
            "author" : {
                "id" : ObjectId("56718e642b464a35593d7f21"),
                "name" : "xxxx",
                "email" : "xxxx@xxxx.com"
            }
        }
    ],
    "title" : "This is a great post from haven"
}
{
    "_id" : ObjectId("567191932b464a35593d7f24"),
    "text" : "",
    "tags" : [ ],
    "comments" : [
        {
            "text" : "This is a test comment",
            "created_on" : ISODate("2015-12-16T16:17:22.912Z"),
            "id" : ObjectId("56718e922b464a35593d7f22"),
            "author" : {
                "id" : ObjectId("56718e642b464a35593d7f21"),
                "name" : "xxx",
                "email" : "xxx@xxxx.com"
            }
        }
    ],
    "title" : "I like cake"
}

一些环境信息:Django 1.5.11; Python 2.7.6;数据库:django_mongodb_engine;操作系统:Ubuntu-14.04

我尝试在django shell(激活virtualenv)插入评论,作者和帖子。一切都运作良好。我仔细检查了mongo shell,发现了我使用django shell插入的所有数据。这是我从mongo shell获得的db.blogs_post.find()。pretty():

{{1}}

这些是我使用django shell插入的数据。但是,当我运行Post.objects.all()时,它返回一个空列表。但是Comment.objects.all()和Author.objects.all()会返回数据库中的内容。

我正在考虑将MongoDB用于实际的金融项目,因此非常感谢任何有关如何解决这个问题的想法。

由于

0 个答案:

没有答案