直到最近一直工作完美的東西已停止工作。在另一個對象中有一個外鍵的模型在標準的django管理中不可編輯。問題是這樣的:ForeignKeys某些模型在de admin界面中是不可編輯的,雖然它們應該是,儘管標籤確實出現在管理頁面中。 它看起來像這樣:在Django中不可編輯的外鍵Admin
對位的HTML代碼,其中外鍵的故事模型應該編輯:
<div class="form-row field-story">
<div>
<label for="id_story" class="required">Story:</label>
</div>
</div>
2相關機型各自的的Unicode DEFS:
class Question(models.Model):
question = models.CharField(max_length = 200)#the Actual question
correct_answer = models.ForeignKey(Answer, verbose_name = 'Correct Answer', related_name = 'Correct answer')
incorrect_answers = models.ManyToManyField(Answer, verbose_name = 'Distractor options', related_name = 'Incorrect answers')
story = models.ForeignKey(Story)
def __unicode__(self):
try:
return self.question.decode('latin-1') + '(%i)'%(self.id)
except:
return str(self.id)
class Story(models.Model):
class Meta:
verbose_name_plural = 'Stories'
author = models.ForeignKey(User, blank = True, null = True, on_delete = models.SET_NULL, editable = False)
name = models.CharField(max_length = 150, verbose_name = 'Name/Summary')
story = models.TextField(verbose_name = 'Story', max_length = 13 * 54)#the actual story
publish = models.BooleanField(default = False, help_text = 'If this is published, it will be uneditable.')
date_added = models.DateTimeField (auto_now_add = True, editable = False)#date of reply
ready = models.BooleanField(default = False, help_text = 'Whether the author thinks this is ready')
erf = models.CharField(max_length = 20, verbose_name = 'ERF label', editable = False, blank = True, null = True)
def __unicode__(self):
try:
return "'"+self.name.encode('latin-1') + "'"
except:
return "Story with unicode name or something: %i" %(self.id)
在admin.py:
admin.site.register(Question,)
看看什麼可行,哪些不行,我開始覺得它與故事模型有關。其他外鍵關係運行良好。當然,foreignkey不可編輯的事實意味着即使MySQL顯示問題表中有一個有效的story_id,該對象也不能從管理員保存。
我記得使用過這個,並且這個工作正常。我懷疑unicode問題的地方,但我無法想象究竟是什麼,更不用說如何解決它們。任何人都可以幫忙嗎?
PS。我認爲,ForeignKey field will not appear in Django admin site發生的情況並非如此。