Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type object 'X' has no attribute 'DoesNotExist' with django

Tags:

python

django

I keep getting the has no attribute DoesNotExist error.

Any ideas why?

So far I tried:

    try:
        current_report = Report.objects.get(account_profile=current_profile)
    except Report.DoesNotExist:
        print("report doesn't exist")
        current_report=None

And my debug shows type object 'Report' has no attribute 'DoesNotExist' at line current_report(etc):

I also tried:

from django.core.exceptions import ObjectDoesNotExist
...
except Report.ObjectDoesNotExist: 

and

try:
    Report.objects.get(account_profile=current_profile)
except Report.DoesNotExist:
    print("report doesn't exist")
    current_report=None

and

try:
    Report.objects.get(account_profile=current_profile)
except ObjectDoesNotExist:
    print("report doesn't exist")
    current_report=None

Why does type object 'X' have no attribute 'DoesNotExist'? I'm using django.

In my Models.py I have:

class Report(models.Model):
    account_profile = models.ForeignKey(Profile)
    total_visitors = models.CharField(max_length=200, blank=True, null=True)
    last_week_visitors = models.CharField(max_length=200, blank=True, null=True)
    new_visitors_this_wk = models.CharField(max_length=200, blank=True, null=True)
    new_visitors_last_wk = models.CharField(max_length=200, blank=True, null=True)
    bounce_rate = models.CharField(max_length=200, blank=True, null=True)
    last_week_bounce_rate = models.CharField(max_length=200, blank=True, null=True)
    percent_new_referrals = models.CharField(max_length=100, blank=True, null=True)
    last_week_new_referrals = models.CharField(max_length=100, blank=True, null=True)
    this_week_pg_load_time = models.CharField(max_length=100, blank=True, null=True)
    last_week_pg_load_time = models.CharField(max_length=100, blank=True, null=True)
    date_created = models.DateTimeField(default=datetime.now, blank=True)
    week_number = models.CharField(max_length=10, blank=True, null=True)

    #HTML table for browsers with avg session durations less than 10 seconds
    sessions_vs_browser = models.TextField(blank=True, null=True)
    sessions_vs_country = models.TextField(blank=True, null=True)
    sessions_vs_device = models.TextField(blank=True, null=True)
    total_sessions = models.CharField(max_length=100, blank=True, null=True)
    keywords = models.ManyToManyField(Keyword)
    referrals = models.ManyToManyField(Referral)
    pages_speeds = models.ManyToManyField(PageSpeed)
    bounces = models.ManyToManyField(BouncePage)
like image 447
Costantin Avatar asked Feb 16 '17 20:02

Costantin


2 Answers

Import the exception,

from django.core.exceptions import ObjectDoesNotExist

And catch it

try:
    Report.objects.get(account_profile=current_profile)
except ObjectDoesNotExist:
    print("report doesn't exist")
    current_report=None

Because ObjectDoesNotExist is a Django specific exception and you have to import it in order to catch it.

Also it is not a property of the model to do Model.ObjectDoesNotExist

like image 86
All Іѕ Vаиітy Avatar answered Oct 06 '22 01:10

All Іѕ Vаиітy


I was also facing this kind of behavior, the problem was having the same View name and Model name.You only need to change the View name.

You can handle exception in both way using

  1. Model specific exception.
  2. django.core.exceptions.ObjectDoesNotExist

try:
     Report.objects.get(account_profile=current_profile)
except Report.DoesNotExist:
     print("report doesn't exist")
     current_report=None

or

try:
     Report.objects.get(account_profile=current_profile)
except ObjectDoesNotExist:
     print("report doesn't exist")
     current_report=None

DoesNotExist

exception Model.DoesNotExist

This exception is raised by the ORM in a couple places, for example by QuerySet.get() when an object is not found for the given query parameters. 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. The exception is a subclass of django.core.exceptions.ObjectDoesNotExist.

like image 29
Muhammad Faizan Fareed Avatar answered Oct 06 '22 00:10

Muhammad Faizan Fareed