Is it possible to use objects as keys to a dictionary in django? I did it and it works. But I wonder if this is the best practice, or if it will be difficult, I do not foresee right now.
I am working on a project that addresses educational standards. I have dictionaries with a structure along the lines {Subject:[Standards]} . The model for the object looks something like this:
class Subject(models.Model): subject = models.CharField(max_length=255, unique=True) def __unicode__(self): return self.subject
Is it possible to use objects from this model as keys to my dictionaries, or should I use a string representation like Subject.subject?
If so, does the unicode method affect it? When I tried to use Subject.subject as a 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 question for the question that I asked yesterday about using None as a dictionary key .
source share