jap*_*hyr 6 python django dictionary key
在django中使用对象作为字典的键是否合理?我这样做了,它的确有效.但我想知道这是不是最好的做法,或者是否会造成我现在还没有预见到的困难.
我正在研究一个处理教育标准的项目.我的字典有一个结构{Subject:[Standards]}.主题的模型看起来像:
class Subject(models.Model):
subject = models.CharField(max_length=255, unique=True)
def __unicode__(self):
return self.subject
Run Code Online (Sandbox Code Playgroud)
是否可以使用此模型中的对象作为我的词典的键,或者我应该使用字符串表示,例如Subject.subject?
如果是这样,unicode方法会影响这个吗?当我尝试使用Subject.subject作为键时,我得到了像{u'Math': [<Subject: Students can perform calculations.>]} 使用对象作为键的东西,它看起来像{<Subject: Math>: [<Standard: Students can perform calculations.>]}
这是我昨天问过的关于使用None作为字典键的问题的后续内容.