【问题标题】:Django models.Manager unable to access modelDjango models.Manager 无法访问模型
【发布时间】:2020-06-05 15:57:10
【问题描述】:

我有以下测试失败,因为它只在数据库中插入一行,它应该插入 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 会自动生成一个名为 objects 的管理器

【问题讨论】:

    标签: django django-models django-managers


    【解决方案1】:

    您每次都保存相同 Questions 对象,因此在第一次创建它之后,您更新 现有对象。最后,只有一个。

    您可以在循环中创建一个新对象:

    class QuestionsTest(TestCase):
        def setUp(self):
            self.fake = Faker()
    
        def test_populating_table_with_random_data(self):
            x = 0
            while x < 100:
                newQuestion = Questions()
                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(100, Questions.objects.count())

    使用for 循环可能会更好:

    class QuestionsTest(TestCase):
        def setUp(self):
            self.fake = Faker()
    
        def test_populating_table_with_random_data(self):
            for __ in range(100):
                newQuestion = Questions()
                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()
            #100 rows should be inserted
            self.assertEqual(100, Questions.objects.count())

    你也可以用.objects.create(…)创建对象:

    class QuestionsTest(TestCase):
        def setUp(self):
            self.fake = Faker()
    
        def test_populating_table_with_random_data(self):
            for __ in range(100):
                newQuestion = Questions.objects.create(
                    category = self.fake.text(max_nb_chars=254),
                    difficulty = self.fake.text(max_nb_chars=8),
                    question_type = self.fake.text(max_nb_chars=20),
                    text = self.fake.text(max_nb_chars=254)
                )
            #100 rows should be inserted
            self.assertEqual(100, Questions.objects.count())

    【讨论】:

    • 您的回答完美。您能否详细说明为什么使用for 比使用while 更好?您在for 循环中使用__ 是否有任何具体原因
    • @0kImightBeAble: 因为使用for 更难进入无限循环,因为它不断迭代迭代。您不需要初始化、检查和更新变量。 __ 通常被用作“丢弃”变量,因此我们不关心变量,但必须使用它才能使其有效 Python。
    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 2011-08-19
    • 2017-09-12
    • 2013-10-15
    • 2015-08-27
    • 2019-01-31
    • 2014-09-16
    • 2012-12-15
    相关资源
    最近更新 更多