【问题标题】:Django: object has no attribute 'was_published_recently'Django:对象没有属性'was_published_recently'
【发布时间】:2016-11-04 14:12:47
【问题描述】:

刚接触 Django,我一直在做第一个教程,现在我在第 5 部分,即自动化测试。

按照教程进行到步骤“修复Bug”后,我运行测试时弹出错误,如下:

   Traceback (most recent call last):
   File "D:\Python\Django\ui1\polls\tests.py", line 13, in test_was_published_recently_with_future_question
    self.assertIs(future_question.was_published_recently(), False)
AttributeError: 'Question' object has no attribute 'was_published_recently'

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
Destroying test database for alias 'default'...

而在教程页面中,它在测试中没有显示错误。

Creating test database for alias 'default'...
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
Destroying test database for alias 'default'...

这是我的代码。

tests.py

import datetime

from django.utils import timezone
from django.test import TestCase

from .models import Question

class QuestionMethodTests(TestCase):
    def test_was_published_recently_with_future_question(self):
#should return False for questions whose pub_date is in the future.
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)

models.py

from django.db import models

class Question(models.Model):
    question_text= models.CharField(max_length=200)
    pub_date=models.DateTimeField('date published')
    def __str__(self):
        return self.question_text

class Choice(models.Model):
    question= models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.choice_text

def was_published_recently(self):
    now = timezone.now()
    return now - datetime.timedelta(days=1) <= self.pub_date <= now

【问题讨论】:

    标签: django django-models django-tests


    【解决方案1】:

    您现在需要将您的函数移到不属于 Question 类的类中。

    class Question(models.Model):
        ...
    
    
        def was_published_recently(self):
            ...
    

    【讨论】:

    • 只需要导入日期时间和时区,然后照你说的做,谢谢。
    【解决方案2】:

    models.py 中将 Question 模型替换为以下代码 -

    class Question(models.Model):
        question_text = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
    
        def __str__(self):
            return self.question_text
    
        def was_published_recently(self):
            return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    

    另外,请确保您已导入时区 -

    from django.utils import timezone 
    

    【讨论】:

      【解决方案3】:

      models.py 中添加:

      from django.db import models
      from django.utils import timezone
      

      【讨论】:

        【解决方案4】:

        退出python shell(quit()或exit())并重新启动它然后再次尝试您的代码的最佳方法。然后就执行了。

        【讨论】:

          猜你喜欢
          • 2020-02-11
          • 2015-02-19
          • 2016-03-30
          • 2021-04-05
          • 2013-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多