向现有的 django Mongoengine 模型添加了新的布尔字段,但之后无法过滤该字段
首先,我的 django 模型是这样的:
class List(Document):
owner = ReferenceField('User')
name = StringField()
users = ListField(ReferenceField('User'))
created_at = DateTimeField(default=datetime.datetime.now)
在我添加了一个新的归档 is_cancelled 之后,现在是这样的:
class List(Document):
owner = ReferenceField('User')
name = StringField()
users = ListField(ReferenceField('User'))
created_at = DateTimeField(default=datetime.datetime.now)
is_cancelled = BooleanField(default = False)
我使用 mongoengine 来实现 django mongodb ORM。但现在当我想进行过滤查询时:
List.objects.filter(is_cancelled=False)
returns []
我使用 django 对象将所有 is_cancelled 字段设置为 False:
for x in List.objects.all():
x.is_cancelled = False
x.save()
但我仍然得到上面查询的空列表。 我正在寻找 django 对象的 is_cancelled 归档,我看到 is_cancelled = False
l = List.objects.all()[0]
l.is_cancelled
False
但是当我从 mongodb shell 中查看时。没有归档为 is_cancelled。
db.list.find()
{ "_cls" : "List", "_id" : ObjectId("4e8451598ebfa80228000000"), "_types" : [ "List" ],
"created_at" : ISODate("2011-09-29T16:24:28.781Z"), "name" : "listname", "users" : [
{
"$ref" : "user",
"$id" : ObjectId("4e79caf78ebfa80c00000001")
}, {
"$ref" : "user",
"$id" : ObjectId("4e79e4df8ebfa80b64000001")
}, {
"$ref" : "user",
"$id" : ObjectId("4e7aeb898ebfa80b64000001")
}, {
"$ref" : "user",
"$id" : ObjectId("4e79ce028ebfa80c00000004")
} ] }
我该如何解决这个查询
First my django model was like this:
class List(Document):
owner = ReferenceField('User')
name = StringField()
users = ListField(ReferenceField('User'))
created_at = DateTimeField(default=datetime.datetime.now)
After I added a new filed is_cancelled and now it is like that:
class List(Document):
owner = ReferenceField('User')
name = StringField()
users = ListField(ReferenceField('User'))
created_at = DateTimeField(default=datetime.datetime.now)
is_cancelled = BooleanField(default = False)
I use mongoengine for django mongodb ORM. But now when i want to make a filter query:
List.objects.filter(is_cancelled=False)
returns []
I make all is_cancelled fields to False with django object:
for x in List.objects.all():
x.is_cancelled = False
x.save()
But I'm still getting an empty list for the query above.
I'm looking a django objects' is_cancelled filed and I see is_cancelled = False
l = List.objects.all()[0]
l.is_cancelled
False
But when I look from mongodb shell. There is no filed as is_cancelled.
db.list.find()
{ "_cls" : "List", "_id" : ObjectId("4e8451598ebfa80228000000"), "_types" : [ "List" ],
"created_at" : ISODate("2011-09-29T16:24:28.781Z"), "name" : "listname", "users" : [
{
"$ref" : "user",
"$id" : ObjectId("4e79caf78ebfa80c00000001")
}, {
"$ref" : "user",
"$id" : ObjectId("4e79e4df8ebfa80b64000001")
}, {
"$ref" : "user",
"$id" : ObjectId("4e7aeb898ebfa80b64000001")
}, {
"$ref" : "user",
"$id" : ObjectId("4e79ce028ebfa80c00000004")
} ] }
How can I fix this query
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
瞧!
这是我的答案:
https://github.com/hmarr/mongoengine/issues/282
mongengine BooleanField 存在值为 False 的错误。
但他们已经用这个补丁修复了它:
https://github.com/hmarr/mongoengine/pull/ 283
Voila!
This is my answer:
https://github.com/hmarr/mongoengine/issues/282
There is a bug at the mongengine BooleanField with a value of False.
But they have fixed it with this patch:
https://github.com/hmarr/mongoengine/pull/283
这是因为 mongoDB 是一个无模式数据库。尽管您已在模型中定义了字段 is_canceled,但这并不意味着所有现有文档都将使用此新字段进行更新。在集合中,每个文档不需要遵循相同的结构。
如果您希望每个现有文档中都有 is_canceled 字段,则需要编写一个更新脚本来迭代集合中的每个文档并添加该字段。否则,只有使用此新模型创建的新文档才会包含字段 is_canceled。
希望这有帮助。
It is because mongoDB is a schema-less database. Although you have defined the field is_canceled in your model, that does not mean all existing documents are going to be updated with this new field. Within a collection, each document is not required to follow the same structure.
If you want the is_canceled field in each of your existing documents you will need to write an update script to iterate through each document in your collection and add that field. Otherwise, only new documents you create with this new model will contain the field is_canceled.
Hope this helps.