Is it reasonable to use objects as keys to a dictionary in django? I have done so and it works. But I am wondering if this is best practice, or if it is going to make difficulties I don't foresee right now.
I am working on a project which deals with educational standards. I have dictionaries with a structure along the lines of {Subject:[Standards]}
. The model for subject looks something like:
class Subject(models.Model):
subject = models.CharField(max_length=255, unique=True)
def __unicode__(self):
return self.subject
Is it okay to use the objects from this model as keys to my dictionaries, or should I be using a string represenation, such as Subject.subject instead?
If so, does the unicode method affect this? When I tried using Subject.subject as the key, I got things like {u'Math': [<Subject: Students can perform calculations.>]}
Using objects as keys, it looks like {<Subject: Math>: [<Standard: Students can perform calculations.>]}
This is a followup to a question I asked yesterday about using None as a dictionary key.