直到最近一直有效的东西已经停止工作。具有另一个对象的外键的模型在标准 django 管理员中无法按预期进行编辑。问题是这样的:某些模型的外键在管理界面中不可编辑,尽管它们应该是,尽管标签确实出现在管理页面中。它看起来像这样:
应该编辑 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 字段中发生的事情不会出现在 Django 管理站点中。