Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

python - Django DoesNotExist

I am having issues on trying to figure "DoesNotExist Errors", I have tried to find the right way for manage the no answer results, however I continue having issues on "DoesNotExist" or "Object hast not Attribute 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')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This line

 except Vehicle.vehicledevice.device.DoesNotExist

means look for device instance for DoesNotExist exception, but there's none, because it's on class level, you want something like

 except Device.DoesNotExist

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...