A question of style (finding item in list of tuples)

Roy Smith roy at panix.com
Mon May 21 11:31:41 EDT 2012


On Monday, May 21, 2012 9:39:59 AM UTC-4, Jon Clements wrote:

> >     def experience_text(self):
> >         return dict(CHOICES).get("self.level", "???")

> Haven't used django in a while, but doesn't the model provide a get_experience_display() method which you could use...

Duh, I totally missed seeing that!  Yeah, that's exactly what I want.  Thanks.  Hmmm, looking at the source, I see they went the dict().get() route:

    def _get_FIELD_display(self, field):
        value = getattr(self, field.attname)
        return force_unicode(dict(field.flatchoices).get(value, value), strings_only=True)



More information about the Python-list mailing list