无法使用 ID 进行查询 - Django MongoDB 引擎

发布于 2024-10-15 04:16:33 字数 1122 浏览 6 评论 0原文

我正在使用 django-nonrel 和 django-mongodb-engine ,

我有一个存储在 PostgreSQL 上的 Django 模型:

class Author(models.Model):
    name = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)

我有一个 MongoDB< /strong> 存储模型:

class Post(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

    author = models.ForeignKey(Author)

每当我尝试按作者 ID 过滤帖子时:

posts = Post.objects.filter(author__id=1)

我收到以下错误:

/usr/local/lib/python2.6/dist-packages/bson/objectid.pyc in __validate(self, oid)
    158                     raise InvalidId("%s is not a valid ObjectId" % oid)
    159             else:
--> 160                 raise InvalidId("%s is not a valid ObjectId" % oid)
    161         else:
    162             raise TypeError("id must be an instance of (str, ObjectId), "

InvalidId: 1 is not a valid ObjectId

In [22]: Post.objects.filter(author__id=1)

有什么想法吗?

I am using django-nonrel and django-mongodb-engine

I have a Django model stored on PostgreSQL:

class Author(models.Model):
    name = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)

I have a MongoDB stored model:

class Post(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

    author = models.ForeignKey(Author)

Whenever I try to filter posts by author id:

posts = Post.objects.filter(author__id=1)

I get the following error:

/usr/local/lib/python2.6/dist-packages/bson/objectid.pyc in __validate(self, oid)
    158                     raise InvalidId("%s is not a valid ObjectId" % oid)
    159             else:
--> 160                 raise InvalidId("%s is not a valid ObjectId" % oid)
    161         else:
    162             raise TypeError("id must be an instance of (str, ObjectId), "

InvalidId: 1 is not a valid ObjectId

In [22]: Post.objects.filter(author__id=1)

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

度的依靠╰つ 2024-10-22 04:16:33

我相信“mongo 方式”是将作者详细信息嵌入帖子中,以及所有评论嵌入文档中。在您的情况下,拥有多个相关集合(表)并不是解决此问题的最佳方法。

尽可能减少外键的使用,只嵌入数据。我相信 MongoEngine 有一个 Document 和 EmbeddedDocument 类,您可以继承。

为什么要使用 mongodb 而不是 mysql?你一定有一个巨大的博客......;-)

I believe the "mongo way" would be to embed the author details in the post, as well as all comments, in Embedded Documents. Having multiple related collections (tables) isn't the best way to solve this problem in your case.

Reduce your foreignKey usage as much as possible, and just embed the data. I believe MongoEngine has a Document and EmbeddedDocument class you can inherit from.

Why do you want to use mongodb vs. mysql? You must have a HUGE blog... ;-)

深爱成瘾 2024-10-22 04:16:33

或许:

author = Author.objects.get(pk=1)
posts = author.post_set.all()

May be:

author = Author.objects.get(pk=1)
posts = author.post_set.all()
三生路 2024-10-22 04:16:33

混合这两个引擎是不可能的。最简单的解释是 MongoDB 主键是类似字符串的对象,而 *SQL pks 是整数。不能两者同时使用,只能选其一。

It's impossible to mix those two engines. The simplest explanation for this is that MongoDB primary keys are string-like objects and *SQL pks are integers. You can't use both, chose one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文