我对django完全陌生,以前是php编码器,所以如果我很笨,请多多包涵。
我在应用程序中定义了三种模型:理解,问题和答案。每个理解都有多个问题和答案,在理解模型中定义为“内联”。问题由管理员直接输入,但答案会根据理解自动添加。
我要实现的是,将理解理解分解为句子,并使用当前理解的外键将每个句子添加为答案对象。
我正在尝试覆盖Comprehension模型中的save方法。但是当我单击保存时,它给出了一个实例错误
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模型的外键在提交条件下创建对象的空白。
最佳答案
您应该使用self
而不是self.pk
并注意self
引用当前对象。
p = Answer.objects.create(AnswerText = sentence, ComprehensionAnswer = self)
从追溯中可以清楚地看出,
ComprehensionAnswer
模型的Answer
属性期望使用Comprehension
模型的对象。但是,您正在传递该对象的ID。关于python - 在Django管理中的for循环中创建另一个模型对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35824099/