DoesNotExist:id(ObjectId)中不存在[Model]匹配查询

时间:2017-01-16 04:56:38

标签: python django mongodb mongoengine

我正在尝试使用其ObjectId查询唯一文档。然而,错误出现了:

  

DoesNotExist:路由匹配查询不存在

在将此视图作为请求传递给我的视图时,它会在ObjectId typeform中打印出相应的ObjectId。因此,行route_test = Route.objects.get(id=_id)不应该出现问题。 我有以下代码:

views.py

def update(request):

    if request.method == "POST":

        _id = request.POST.get('_id',ObjectId())
        print(_id)
        route_id = request.POST.get('route_id','')
        geometry = request.POST.get('geometry', '')
        properties = request.POST.get('properties','')

        #r = Route.objects.get(route_id='LTFRB_PUJ2616') --> I cannot use this
                                      #because it has 5 instances (Not Unique)
        #print (r["id"],r["properties"]) 

        test = Route.objects.get(id = ObjectId('587c4c3b203ada19e8e0ecf6'))
        print (test["id"], test["properties"])

        try:
            route_test = Route.objects.get(id=_id)
            print(route_test)
            Route.objects.get(id=_id).update(set__geometry=geometry, set__properties=properties)
            return HttpResponse("Success!")

        except:
            return HttpResponse("Error!")

AJAX

 var finishBtn = L.easyButton({
     id:'finish',
     states: [{
              icon:"fa fa-check",
              onClick: function(btn){
                  selectedFeature.editing.disable();
                  layer.closePopup();
                  var editedFeature = selectedFeature.toGeoJSON();
                  alert("Updating:" + editedFeature.route_id);

                  $.ajax({
                     url: "/update/",
                     data: {id:editedFeature.id,
                            route_id: JSON.stringify(editedFeature.route_id),
                            geometry: JSON.stringify(editedFeature.geometry),
                            properties: JSON.stringify(editedFeature.properties)
                         }, 
                     type: 'POST'     
            });
 }

model.py

from __future__ import unicode_literals

from mongoengine import *

class Route(Document):
    type = StringField(required=True)
    route_id = StringField(required=True)
    geometry = LineStringField()
    properties = DictField()

    meta = {'collection':'routes'}

该怎么办?即使我直接提供传入test = Route.objects.get(id = ObjectId('587c4c3b203ada19e8e0ecf6'))的行_id也有同样的错误......

0 个答案:

没有答案