我在一个视图中有以下代码:
lp_detail = LearningPathDetail()
pos_in_path = 1
lp_detail.lpheader_id = pk
lesson_ids = request.POST["lesson_ids"].split(",")
for lesson_id in lesson_ids:
lp_detail.id = ""
lp_detail.lesson_id = lesson_id
lp_detail.pos_in_path = pos_in_path
lp_detail.save()
pos_in_path += 1
pk是标题表中的ID,它指向标识与其关联的所有详细信息记录的标题记录.
lesson_ids is a list of DB ids that need to be inserted into the lp_detail table.
What I think the code should do (according to the manual) based on the id being blank (I have also tried setting it to None)
is create the record, but instead I get an error:
Field 'id' expected a number but got ''.
以下是LearningPath Detail表的模型:
class LearningPathDetail(models.Model):
lpheader = models.ForeignKey(LearningPathHeader, on_delete=models.CASCADE)
lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, blank=True, null=True)
pos_in_path = models.IntegerField(default=1)
我不确定我在这里有什么不正确的.