我是 Django 和單元測驗的新手。
我正在為我的應用程式撰寫測驗,這是一個由幾個實驗室組成的學習平臺,其中包括課程和問題(3 種不同型別),問題和實驗室之間的關系是通過外鍵完成的。
我正在嘗試撰寫測驗,在其中向資料庫添加實驗室和多項選擇題。
問題是測驗總是以相同的錯誤結束:
DETAIL: Key (lab_id)=(1) is not present in table "labs_lab".
我理解這句話的意思,但是如果我提供問題的 id 以將它們鏈接到相應的測驗實驗室,怎么會發生這種情況?
謝謝
模型.py
class Lab(models.Model):
lab_name = models.CharField(max_length=200)
category = models.ForeignKey(Category, unique=True, null=True, on_delete=models.PROTECT)
pub_date = models.DateTimeField('date published')
lab_theory = models.TextField()
def __str__(self):
return self.lab_name
class QuestionMultipleChoice(models.Model):
lab = models.ForeignKey(Lab, on_delete=models.CASCADE)
type = QuestionType.multiplechoice
question = models.CharField(max_length=200,null=True)
option1 = models.CharField(max_length=200,null=True)
option2 = models.CharField(max_length=200,null=True)
option3 = models.CharField(max_length=200,null=True)
option4 = models.CharField(max_length=200,null=True)
answer = models.IntegerField(max_length=200,null=True)
def __str__(self):
return self.question
@property
def html_name(self):
return "q_mc_{}".format(self.pk)
@property
def correct_answer(self):
correct_answer_number = int(self.answer)
correct_answer = getattr(self, "option{}".format(correct_answer_number))
return correct_answer
def check_answer(self, given):
return self.correct_answer == given
測驗檔案
class TestData(TestCase):
@classmethod
def setUpTestData(cls):
past_date = date(1997, 3, 2)
future_date = date(2050, 3, 2)
Lab.objects.create(lab_name="testlab", pub_date=datetime.now(), lab_theory="test theory")
Lab.objects.create(lab_name="test lab past question", pub_date=past_date, lab_theory="test lab past question")
Lab.objects.create(lab_name="test lab future question", pub_date=future_date, lab_theory="test lab future question")
class QuestionIndexViewTests(TestCase):
def test_no_questions(self):
"""
If no questions exist, an appropriate message is displayed.
"""
response = self.client.get(reverse('labs:index'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "No labs are available.")
self.assertQuerysetEqual(response.context['latest_question_list'], [])
def test_past_question(self):
"""
Questions with a pub_date in the past are displayed on the
index page.
"""
past_question = QuestionMultipleChoice.objects.create(question='This is a test question', option1='1', option2='2', option3='3', option4='4', answer='1', lab_id=2)
response = self.client.get(reverse('labs:index'))
self.assertQuerysetEqual(
response.context['latest_question_list'],
[past_question],
)
更新:
更新:我改變了測驗,我給了“QuestionMultipleChoice”不同的引數:
question1 = QuestionMultipleChoice.objects.create(lab=1, question='This is a test question', option1='1', option2='2', option3='3', option4='4', answer='1')
現在的結果是:
ValueError: Cannot assign "1": "QuestionMultipleChoice.lab" must be a "Lab" instance.
uj5u.com熱心網友回復:
ValueError:無法分配“1”:“QuestionMultipleChoice.lab”必須是“Lab”實體。
這意味著實驗室屬性試圖獲取實驗室實體,您可以提供實驗室實體來分配實驗室實體,您必須這樣做
lab1 = Lab.objects.create(lab_name="testlab", pub_date=datetime.now(), lab_theory="test theory")
QuestionMultipleChoice.objects.create(question='This is a test question', lab=lab1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/387529.html
