Django 在 json 对象的视图中查询 MongoDB ObjectIds
Django Querying MongoDB ObjectIds in views from json object
我目前正在 Python Django 中查询 MongoDB 对象,如果需要其他属性,我在创建查询时没有遇到任何问题。
但是我需要修改我的查询以专门过滤 ObjectIds return 找到一个或没有找到对象。
从我的 Javascript 我正在将 json 数据传递到我的 Django views.py
这是它当前的样子:
def update(request):
#AJAX data
line = json.loads(request.body)
_id = line['_id']
print("OBJECT_ID: %s" % (_id))
another_id = line['another_id']
print("ANOTHER_ID: %s" % (another_id))
*不要混淆 another_id
,有些对象具有相同的 another_id
,不幸的是必须保持这样。这就是为什么我不能查询它的更新,因为它会更新所有重复项。这就是我需要 ObjectId 的原因。
检查这里是它打印出来的内容:
{u'$oid': u'582fc95bb7abe7943f1a45b2'}
ANOTHER_ID: LTJ1277
因此我在 views.py
中追加了这样的查询:
try:
Line.objects(_id=_id).update(set__geometry=geometry, set__properties=properties)
print("Edited: " + another_id)
except:
print("Unedited.")
但它没有 return 任何对象。
So I was wondering if the query itself can't recognize the $oid
in the json body as "_id" : ObjectId("582fc95bb7abe7943f1a45b2")
?
*编辑:
from bson.objectid import ObjectId
我在其中编辑 views.py
的地方:
_id = line['_id']
print("VALUES: %s" % (_id.get('$oid')))
try:
Line.objects(_id=ObjectId(_id.get('$oid'))).update(set__geometry=geometry, set__properties=properties)
输出:
VALUES: 582fc95bb7abe7943f1a498c
运气不好。仍然没有 querying/not 找到。
根据这个 Using MongoDB with Django 参考站点:
Notice that to access the unique object ID, you use "id" rather than "_id".
我尝试修改以下代码:
Line.objects(_id=ObjectId(_id.get('$oid'))).update(set__geometry=geometry, set__properties=properties)
到
Line.objects(id=ObjectId(_id.get('$oid'))).update(set__geometry=geometry, set__properties=properties)
...现在可以正常使用了。为可能需要这个的其他人保留这个问题。
我目前正在 Python Django 中查询 MongoDB 对象,如果需要其他属性,我在创建查询时没有遇到任何问题。
但是我需要修改我的查询以专门过滤 ObjectIds return 找到一个或没有找到对象。
从我的 Javascript 我正在将 json 数据传递到我的 Django views.py
这是它当前的样子:
def update(request):
#AJAX data
line = json.loads(request.body)
_id = line['_id']
print("OBJECT_ID: %s" % (_id))
another_id = line['another_id']
print("ANOTHER_ID: %s" % (another_id))
*不要混淆 another_id
,有些对象具有相同的 another_id
,不幸的是必须保持这样。这就是为什么我不能查询它的更新,因为它会更新所有重复项。这就是我需要 ObjectId 的原因。
检查这里是它打印出来的内容:
{u'$oid': u'582fc95bb7abe7943f1a45b2'}
ANOTHER_ID: LTJ1277
因此我在 views.py
中追加了这样的查询:
try:
Line.objects(_id=_id).update(set__geometry=geometry, set__properties=properties)
print("Edited: " + another_id)
except:
print("Unedited.")
但它没有 return 任何对象。
So I was wondering if the query itself can't recognize the
$oid
in the json body as"_id" : ObjectId("582fc95bb7abe7943f1a45b2")
?
*编辑:
from bson.objectid import ObjectId
我在其中编辑 views.py
的地方:
_id = line['_id']
print("VALUES: %s" % (_id.get('$oid')))
try:
Line.objects(_id=ObjectId(_id.get('$oid'))).update(set__geometry=geometry, set__properties=properties)
输出:
VALUES: 582fc95bb7abe7943f1a498c
运气不好。仍然没有 querying/not 找到。
根据这个 Using MongoDB with Django 参考站点:
Notice that to access the unique object ID, you use "id" rather than "_id".
我尝试修改以下代码:
Line.objects(_id=ObjectId(_id.get('$oid'))).update(set__geometry=geometry, set__properties=properties)
到
Line.objects(id=ObjectId(_id.get('$oid'))).update(set__geometry=geometry, set__properties=properties)
...现在可以正常使用了。为可能需要这个的其他人保留这个问题。