Django models.Manager 无法访问模型
Django models.Manager unable to access model
我有以下测试失败了,因为它只将一行插入到数据库中,而它应该插入 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 """
在收到此错误之前,我遇到了错误 "Class Questions has no objects member"。我通过明确声明
解决了这个问题
objects = models.Manager()
在我的问题模型中,但我认为 django 自动生成了一个名称为 objects
的管理器
您每次都保存 相同 Questions
对象,因此在第一次创建它之后,您 更新 现有的目的。最后只有一个
您可以在循环中创建一个新对象:
class QuestionsTest(TestCase):
def setUp(self):
self.fake = Faker()
def test_populating_table_with_random_data(self):
x = 0
while x < 100:
<b>newQuestion = Questions()</b>
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 <b>__ in range(100)</b>:
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<b>.objects.create(</b>
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)
<b>)</b>
#100 rows should be inserted
self.assertEqual(100, Questions.objects.count())
我有以下测试失败了,因为它只将一行插入到数据库中,而它应该插入 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 """
在收到此错误之前,我遇到了错误 "Class Questions has no objects member"。我通过明确声明
解决了这个问题objects = models.Manager()
在我的问题模型中,但我认为 django 自动生成了一个名称为 objects
的管理器您每次都保存 相同 Questions
对象,因此在第一次创建它之后,您 更新 现有的目的。最后只有一个
您可以在循环中创建一个新对象:
class QuestionsTest(TestCase):
def setUp(self):
self.fake = Faker()
def test_populating_table_with_random_data(self):
x = 0
while x < 100:
<b>newQuestion = Questions()</b>
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 <b>__ in range(100)</b>:
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<b>.objects.create(</b>
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)
<b>)</b>
#100 rows should be inserted
self.assertEqual(100, Questions.objects.count())