直到最近才使用完美的东西已停止工作。在标准django admin中,具有与另一个对象的外键的模型不可编辑。问题是这样:对某些模型的ForeignKeys在de admin界面中是不可编辑的,尽管它们应该是,尽管标签确实出现在管理页面中。 它看起来像这样:
应该编辑Story-model的外键的位的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, )
看看什么有效,什么无效,我开始觉得它与故事模型有关。其他外键关系运作良好。当然,外键不可编辑的事实意味着无法从管理员保存对象,即使MySQL显示问题表中存在有效的story_id。
我记得曾经使用过这个并且这个工作正常。我怀疑某处出现了unicode问题,但我无法想象究竟是什么,更不用说如何修复它们了。有人可以帮忙吗?
PS。我想,在ForeignKey field will not appear in Django admin site发生的事情并非如此。
答案 0 :(得分:1)
Urgh ..在寻找其他异地问题时,我遇到了ForeignKey field problem in Django。在对问题的评论中,Daniel Roseman表明模型上的unicode defs应返回unicode 。试过,我的问题解决了。
Grrr .. Unicode ......你这次赢了!