无法使用 ID 进行查询 - Django MongoDB 引擎
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信“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... ;-)
或许:
May be:
混合这两个引擎是不可能的。最简单的解释是 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.