2016-03-06 56 views
2

我對django完全陌生,以前是一個php編碼器,所以如果我愚蠢,請耐心等待。 我有三個模型定義在我的應用程序,理解,問題,答案。每個理解都有多個問題和答案,在理解模型中定義爲「內聯」。問題由管理員直接輸入,但答案會從理解中自動添加。 我想要實現的是將理解分解爲句子,並將每個句子作爲答案對象與當前理解的外鍵相加。 我想重寫Comprehension模型中的保存方法。但是當我點擊保存時,它會給出實例錯誤在django admin中爲for循環創建另一個模型對象

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模型的外鍵創建提交條件下的對象。

回答

3

您應該使用self而不是self.pk並注意self引用當前對象。

p = Answer.objects.create(AnswerText = sentence, ComprehensionAnswer = self) 

從回溯,它清楚地表明,Answer模式ComprehensionAnswer屬性預計Comprehension模型的對象。但是你傳遞了該對象的ID。

+0

哇,你救了我的日子@Avinash拉傑。自從早上起,我一直在對不同的方法進行洗牌,現在已經有了結果。非常感謝 –

+0

很高興它解決了:-) –

+0

作爲一名以前的php編碼器,我認爲我必須給主鍵存儲在外鍵中。它的好處是django直接從對象中獲得如此強大的功能。再次感謝。 –

相關問題