Django DoesNotExist
我试图find“DoesNotExist错误”的问题,我试图find正确的方式来pipe理无答案的结果,但是我继续在“DoesNotExist”或“对象没有属性DoestNotExists”的问题
from django.http import HttpResponse from django.contrib.sites.models import Site from django.utils import simplejson from vehicles.models import * from gpstracking.models import * def request_statuses(request): data = [] vehicles = Vehicle.objects.filter() Vehicle.vehicledevice_ for vehicle in vehicles: try: vehicledevice = vehicle.vehicledevice_set.get(is_joined__exact = True) imei = vehicledevice.device.imei try: lastposition = vehicledevice.device.devicetrack_set.latest('date_time_process') altitude = lastposition.altitude latitude = lastposition.latitude longitude = lastposition.longitude date_time_process = lastposition.date_time_process.strftime("%Y-%m-%d %H:%M:%S"), date_time_created = lastposition.created.strftime("%Y-%m-%d %H:%M:%S") except Vehicle.vehicledevice.device.DoesNotExist: lastposition = None altitude = None latitude = None longitude = None date_time_process = None date_time_created = None except Vehicle.DoesNotExist: vehicledevice = None imei = '' item = [ vehicle.vehicle_type.name, imei, altitude, "Lat %s Lng %s" % (latitude, longitude), date_time_process, date_time_created, '', '' ] data.append(item) statuses = { "sEcho": 1, "iTotalRecords": vehicles.count(), "iTotalDisplayRecords": vehicles.count(), "aaData": data } json = simplejson.dumps(statuses) return HttpResponse(json, mimetype='application/json')
这条线
except Vehicle.vehicledevice.device.DoesNotExist
意味着查找设备实例的DoesNotExistexception,但没有,因为它是在课堂上,你想要的东西
except Device.DoesNotExist
我已经在这种方式下使用ObjectDoesNotExistfind了解决这个问题的方法
from django.core.exceptions import ObjectDoesNotExist ...... try: # try something except ObjectDoesNotExist: # do something
在此之后,我的代码正常工作
谢谢任何方式,您的文章帮助我解决我的问题