Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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') 
like image 467
Carlos Avatar asked Apr 24 '13 00:04

Carlos


People also ask

How do I handle exceptions in Django?

Django Exception Classes The base class for DoesNotExist exceptions. If a query does not return any result, this exception is raised. It raises when the requested field does not exist. This exception is raised by a query if only one object is expected, but multiple objects are returned.

Does not exist exception Python?

The DoesNotExist exception is raised when an object is not found for the given parameters of a query. Django provides a DoesNotExist exception as an attribute of each model class to identify the class of object that could not be found and to allow you to catch a particular model class with try/except.

What is integrity error in Django?

This means that your DB is expecting that field to have a value. So when it doesn't you get an error. null. If True, Django will store empty values as NULL in the database. Default is False.


1 Answers

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 
like image 125
Dmitry Shevchenko Avatar answered Sep 28 '22 04:09

Dmitry Shevchenko