Django模型。管理器无法访问模型

问题描述 投票:3回答:1

我有以下测试失败,因为它仅将一行插入数据库,而该数据库应在其中插入100行]

class QuestionsTest(TestCase):
    def setUp(self):
        self.fake = Faker()

    def test_populating_table_with_random_data(self):
        newQuestion = Questions()
        x = 0
        while x < 100:
            newQuestion.category = self.fake.text(max_nb_chars=254)
            newQuestion.difficulty = self.fake.text(max_nb_chars=8)
            newQuestion.question_type = self.fake.text(max_nb_chars=20)
            newQuestion.text = self.fake.text(max_nb_chars=254)
            newQuestion.save()
            x += 1
        #100 rows should be inserted
        self.assertEqual(Questions.objects.count(), (100))


"""Traceback (most recent call last):
  File 'Database/tests.py', line 99, in test_populating_table_with_random_data
    self.assertEqual(Questions.objects.count(), (100))
AssertionError: 1 != 100 """


在收到此错误之前,我收到错误“类问题没有对象成员”。我通过明确声明

解决了这个问题
objects = models.Manager()

在我的Questions模型中,但我认为django会自动使用名称对象生成一个管理器

我有以下测试失败,因为它仅将一行插入数据库中应在其中插入100行的类QuestionsTest(TestCase):def setUp(self):self.fake = Faker()...

django django-models django-managers
1个回答
3
投票

您每次保存same

Questions对象,因此在第一次创建它后,您update
© www.soinside.com 2019 - 2024. All rights reserved.