Django 测试:类型对象没有属性 'objects'
Django Test: type object has no attribute 'objects'
在我的网络应用程序中,我有位置和相应的开放时间。 OpeningHours 模型如下所示:
class OpeningHours(models.Model):
location = models.ForeignKey(
Location, related_name='hours', on_delete=models.CASCADE)
weekday = models.PositiveSmallIntegerField(choices=WEEKDAYS, unique=True)
from_hour = models.PositiveSmallIntegerField(choices=HOUR_OF_DAY_12)
to_hour = models.PositiveSmallIntegerField(choices=HOUR_OF_DAY_12)
class Meta:
ordering = ('weekday', 'from_hour')
unique_together = ('weekday', 'from_hour', 'to_hour')
def get_weekday_display(self):
return WEEKDAYS[self.weekday][1]
def get_hours_display(self):
return '{} - {}'.format(HOUR_OF_DAY_12[self.from_hour][1], HOUR_OF_DAY_12[self.to_hour][1])
def get_start_hour_display(self):
return HOUR_OF_DAY_12[self.from_hour][1]
def get_end_hour_display(self):
return HOUR_OF_DAY_12[self.to_hour][1]
def __str__(self):
return '{}: {} - {}'.format(self.get_weekday_display(),
HOUR_OF_DAY_12[self.from_hour][1], HOUR_OF_DAY_12[self.to_hour][1])
我正在尝试测试一个类似于我在我的应用程序中成功测试其他模型的模型:
class OpeningHours(TestCase):
def create_opening_hours(self, weekday=1, from_hour=12, to_hour=15):
self.location = create_location(self)
return OpeningHours.objects.create(location=self.location, weekday=weekday, from_hour=from_hour, to_hour=to_hour)
def test_opening_hours(self):
oh = self.create_opening_hours()
self.assertTrue(isinstance(oh, OpeningHours))
self.assertTrue(0 <= oh.from_hour <= 23)
self.assertTrue(0 <= oh.to_hour <= 23)
self.assertTrue(1 <= oh.weekday <= 7)
,但是当 运行 测试时,我收到此错误消息:
Traceback (most recent call last):
File "<app_path>/tests.py", line 137, in test_opening_hours
oh = self.create_opening_hours()
File "<app_path>/tests.py", line 134, in create_opening_hours
return OpeningHours.objects.create(location=self.location, weekday=weekday, from_hour=from_hour, to_hour=to_hour)
AttributeError: type object 'OpeningHours' has no attribute 'objects'
我认为这可能与 ordering
或 unique_together
元数据有关,但不确定如何解决这个问题...非常感谢任何指向正确方向的指示。
您已为测试 class 和模型 class 命名 OpeningHours
。因此,将测试名称 class 更改为 OpeningHours
以外的任何名称都可以解决上述问题。
在我的网络应用程序中,我有位置和相应的开放时间。 OpeningHours 模型如下所示:
class OpeningHours(models.Model):
location = models.ForeignKey(
Location, related_name='hours', on_delete=models.CASCADE)
weekday = models.PositiveSmallIntegerField(choices=WEEKDAYS, unique=True)
from_hour = models.PositiveSmallIntegerField(choices=HOUR_OF_DAY_12)
to_hour = models.PositiveSmallIntegerField(choices=HOUR_OF_DAY_12)
class Meta:
ordering = ('weekday', 'from_hour')
unique_together = ('weekday', 'from_hour', 'to_hour')
def get_weekday_display(self):
return WEEKDAYS[self.weekday][1]
def get_hours_display(self):
return '{} - {}'.format(HOUR_OF_DAY_12[self.from_hour][1], HOUR_OF_DAY_12[self.to_hour][1])
def get_start_hour_display(self):
return HOUR_OF_DAY_12[self.from_hour][1]
def get_end_hour_display(self):
return HOUR_OF_DAY_12[self.to_hour][1]
def __str__(self):
return '{}: {} - {}'.format(self.get_weekday_display(),
HOUR_OF_DAY_12[self.from_hour][1], HOUR_OF_DAY_12[self.to_hour][1])
我正在尝试测试一个类似于我在我的应用程序中成功测试其他模型的模型:
class OpeningHours(TestCase):
def create_opening_hours(self, weekday=1, from_hour=12, to_hour=15):
self.location = create_location(self)
return OpeningHours.objects.create(location=self.location, weekday=weekday, from_hour=from_hour, to_hour=to_hour)
def test_opening_hours(self):
oh = self.create_opening_hours()
self.assertTrue(isinstance(oh, OpeningHours))
self.assertTrue(0 <= oh.from_hour <= 23)
self.assertTrue(0 <= oh.to_hour <= 23)
self.assertTrue(1 <= oh.weekday <= 7)
,但是当 运行 测试时,我收到此错误消息:
Traceback (most recent call last):
File "<app_path>/tests.py", line 137, in test_opening_hours
oh = self.create_opening_hours()
File "<app_path>/tests.py", line 134, in create_opening_hours
return OpeningHours.objects.create(location=self.location, weekday=weekday, from_hour=from_hour, to_hour=to_hour)
AttributeError: type object 'OpeningHours' has no attribute 'objects'
我认为这可能与 ordering
或 unique_together
元数据有关,但不确定如何解决这个问题...非常感谢任何指向正确方向的指示。
您已为测试 class 和模型 class 命名 OpeningHours
。因此,将测试名称 class 更改为 OpeningHours
以外的任何名称都可以解决上述问题。