我是django的新手,以前是一个php编码器,所以如果我愚蠢的话,请耐心等待。 我在我的应用程序中定义了三个模型,理解,问题,答案。每个理解都有多个问题和答案,定义为' inline'在理解模型中。问题由管理员直接输入,但答案会自动从理解中添加。 我想要实现的是,将理解分成句子并将每个句子添加为当前理解的外键的答案对象。 我试图覆盖理解模型中的保存方法。但是,当我单击“保存”时,它会显示实例错误
Cannot assign "23L": "Answer.ComprehensionAnswer" must be a "Comprehension" instance.
如何在此处分配/创建和实例?或者我采取了错误的做法。如果是这样,请指导我采用正确的方法。
以下是models.py
的内容class Question(models.Model):
QuestionText = models.CharField(max_length=500, verbose_name='Question Text')
QuestionTypeID = models.ManyToManyField(QuestionType, verbose_name='Question Type')
ComprehensionQuestion = models.ForeignKey(Comprehension, verbose_name='comprehension')
QuestionRemarks = models.CharField(max_length=500, verbose_name='remarks', null=True, blank=True)
LastUpdate = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.QuestionText
def was_published_recently(self):
return self.LastUpdate >= timezone.now() - datetime.timedelta(1)
class Answer(models.Model):
AnswerText = models.CharField(max_length=500, verbose_name='Answer Text')
AnswerTypeID = models.ManyToManyField(AnswerType, verbose_name='Answer Type')
ComprehensionAnswer = models.ForeignKey(Comprehension, verbose_name='Comprehension', null=True, blank=True)
AnswerRemarks = models.CharField(max_length=500, verbose_name='Remarks')
LastUpdate = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.AnswerText
class Comprehension(models.Model):
ComprehensionTitle = models.CharField(max_length=100, verbose_name='Comprehension Title')
ComprehensionsText = models.TextField(verbose_name='Text')
ComprehensionsRemarks = models.CharField(max_length=400, verbose_name='Remarks for this Comprehension', null=True, blank=True)
LastUpdate = models.DateTimeField("Last Updated", auto_now=True)
def __unicode__(self):
return self.ComprehensionTitle
def was_published_recently(self):
return self.LastUpdate >= timezone.now() - datetime.timedelta(1)
def save(self, *args, **kwargs):
#overrides the default save function to split the comprehension paragraph into sentences and adds them as probable answers
AnswerList = self.ComprehensionsText.split("u'\u0964'")
for sentence in AnswerList:
p = Answer.objects.create(AnswerText = sentence, ComprehensionAnswer = self.pk)
super(Comprehension, self).save(*args, **kwargs)
admin.py中的内容
class ComprehensionAdmin(admin.ModelAdmin):
form = ComprehensionForm
fieldsets = [
('Main', {'fields': ['ComprehensionTitle','ComprehensionsText']}),
('Additional Info', {'fields': ['ComprehensionsRemarks'], 'classes': ['collapse']}),
]
inlines = [QuestionInline, AnswerInline]
list_display = ('ComprehensionTitle', 'LastUpdate')
list_per_page = 10
class QuestionInline(admin.TabularInline):
model = Question
extra = 2
class AnswerInline(admin.TabularInline):
model = Answer
extra = 2
admin.site.register(Question)
admin.site.register(Answer)
admin.site.register(Comprehension, ComprehensionAdmin)
我也遵循了page中提到的方法。但是,关于如何使用Comprehension model的外键在提交条件下创建对象的空白。
答案 0 :(得分:3)
您应该使用self
代替self.pk
,并注意self
引用当前对象。
p = Answer.objects.create(AnswerText = sentence, ComprehensionAnswer = self)
从回溯中,它清楚地表明,ComprehensionAnswer
模型的Answer
属性需要Comprehension
模型的对象。但是你传递了那个对象的id。