Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #468 -- Model classes now get an accessor method to get the hum…

…an-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
  • Loading branch information...
commit 68c074200807a967b71aa798d7908d0b9c06b3c7 1 parent 272eab5
@adrianholovaty adrianholovaty authored
Showing with 10 additions and 0 deletions.
  1. +10 −0 django/core/meta/__init__.py
View
10 django/core/meta/__init__.py
@@ -592,6 +592,10 @@ def __new__(cls, name, bases, attrs):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.