From 68c074200807a967b71aa798d7908d0b9c06b3c7 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sun, 25 Sep 2005 20:36:05 +0000 Subject: [PATCH] Fixed #468 -- Model classes now get an accessor method to get the human-readable value for each field that has 'choices' set. Thanks, Robert git-svn-id: http://code.djangoproject.com/svn/django/trunk@687 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/meta/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py index cf9284f480..751c5edfbb 100644 --- a/django/core/meta/__init__.py +++ b/django/core/meta/__init__.py @@ -592,6 +592,10 @@ class ModelBase(type): new_mod.get_latest = curry(function_get_latest, opts, new_class, does_not_exist_exception) for f in opts.fields: + if f.choices: + # Add "get_thingie_display" method to get human-readable value. + func = curry(method_get_display_value, f) + setattr(new_class, 'get_%s_display' % f.name, func) if isinstance(f, DateField) or isinstance(f, DateTimeField): # Add "get_next_by_thingie" and "get_previous_by_thingie" methods # for all DateFields and DateTimeFields that cannot be null. @@ -990,6 +994,12 @@ def method_get_next_or_previous(get_object_func, field, is_next, self, **kwargs) kwargs['limit'] = 1 return get_object_func(**kwargs) +# CHOICE-RELATED METHODS ################### + +def method_get_display_value(field, self): + value = getattr(self, field.column) + return dict(field.choices).get(value, value) + # FILE-RELATED METHODS ##################### def method_get_file_filename(field, self):