2017-02-16 38 views
0

has no attribute DoesNotExistエラーが発生します。タイプオブジェクト 'X'にはdjangoの 'DoesNotExist'属性がありません

なぜでしょうか?

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

をそして、私のデバッグは、ラインcurrent_report(など)でtype object 'Report' has no attribute 'DoesNotExist' を示しています:

は、これまでのところ私が試した私も試した

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) 
+1

を行うには、モデルのプロパティではありません、あなたはレポートがこちらのモデルクラスであることを確認していますか?正しいものを輸入しましたか? –

+0

エラーメッセージは文字どおり正しいです。 'Report'はその名前の属性を持っていません(定義または継承のいずれか)。なぜそれが属性を持つべきなのか、まさに正しいエラーメッセージが混乱しているのはなぜか分かりません。 – jpmc26

答えて

5

輸入例外、

from django.core.exceptions import ObjectDoesNotExist 

そしてObjectDoesNotExistは、Djangoの特定の例外であり、あなたが持っているので

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

をそれをキャッチそれをキャッチするためにそれをインポートする。

また、Model.ObjectDoesNotExist

+0

はい、私はそれをインポートした後、 '私は(...)を試した後にチェックしてください'エラーを取得しました: 'AttributeError at/viewreport/ タイプのオブジェクト 'Report'に属性 'オブジェクト'がありません – Costantin

+1

ModelManager、 Modelを正しくインポートしたかどうかを確認し、モデルを上書きしたかどうかを確認してください。 –

+0

いいえ私はDjangoを初めて使っているので、ModelManagerを変更するような大きなカスタマイズはしていません – Costantin

関連する問題