类型对象'X'没有带有django的属性'DoesNotExist'

时间:2017-02-16 20:53:37

标签: python django

我一直收到has no attribute DoesNotExist错误。

任何想法为什么?

到目前为止,我试过了:

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

我的调试显示type object 'Report' has no attribute 'DoesNotExist' 在线current_report(等):

我也尝试过:

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

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

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

为什么类型对象'X'没有属性'DoesNotExist'?我正在使用django。

在我的Models.py中我有:

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)

2 个答案:

答案 0 :(得分:7)

导入例外,

from django.core.exceptions import ObjectDoesNotExist

抓住它

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

因为ObjectDoesNotExist是Django特定的异常,你必须导入它才能捕获它。

同样,Model.ObjectDoesNotExist

不是模型的属性

答案 1 :(得分:0)

我也面临这种行为,问题是视图名称和模型名称相同。您只需更改视图名称。

您可以使用

两种方式处理异常
  1. 特定于模型的异常。
  2. django.core.exceptions.ObjectDoesNotExist

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

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

不存在

异常模型.DoesNotExist

  

ORM在几个地方引发了此异常,例如QuerySet.get()   当找不到给定查询参数的对象时。   Django提供了DidNotExist异常作为每个模型类的属性,以标识找不到的对象类   并允许您使用try / except捕获特定的模型类。   唯一的例外是   django.core.exceptions.ObjectDoesNotExist。