【发布时间】:2011-10-25 22:21:51
【问题描述】:
在 django 中使用对象作为字典的键是否合理?我已经这样做了,并且有效。但我想知道这是否是最佳做法,或者它是否会带来我现在无法预见的困难。
我正在从事一个涉及教育标准的项目。我的字典结构类似于{Subject:[Standards]}。主题的模型如下所示:
class Subject(models.Model):
subject = models.CharField(max_length=255, unique=True)
def __unicode__(self):
return self.subject
可以使用此模型中的对象作为我的字典的键,还是应该使用字符串表示,例如 Subject.subject?
如果是这样,unicode 方法会影响这个吗?当我尝试使用 Subject.subject 作为键时,我得到了类似 {u'Math': [<Subject: Students can perform calculations.>]} 使用对象作为键的东西,它看起来像 {<Subject: Math>: [<Standard: Students can perform calculations.>]}
这是我昨天提出的关于 using None as a dictionary key 的问题的后续。
【问题讨论】:
标签: python django dictionary key