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

Django模型。管理器无法访问模型,django,django-models,django-managers,Django,Django Models,Django Managers,我有一个失败的测试,因为它只在数据库中插入了一行,应该插入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:

我有一个失败的测试,因为它只在数据库中插入了一行,应该插入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 """


在我的问题模型中,但我认为django会自动生成一个名为objects的管理器,您每次保存相同的
问题
对象,因此在第一次创建它之后,您会更新现有对象。最后,只有一个

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

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())
您还可以使用
.objects创建对象。创建(…)

类问题测试(TestCase):
def设置(自):
self.fake=Faker()
def测试用随机数据填充表格(自身):
对于范围内的(100):
newQuestion=Questions.objects.create(
类别=self.fake.text(最大字符数=254),
难度=self.fake.text(最大字符数=8),
问题类型=self.fake.text(最大字符数=20),
text=self.fake.text(最大字符数=254)
)
#应插入100行

self.assertEqual(100,Questions.objects.count())
您的答案非常有效。您能否详细说明为什么使用
for
优于
while
以及您在for中使用
\uuu
的具体原因loop@0kImightBeAble:因为
很难进入无限循环,因为它一直在iterable上迭代。您不需要初始化、检查和更新变量。
\uuu
经常被用作“丢弃”变量,因此我们不关心这个变量,但必须使用它才能使其有效。
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())
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())
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())