Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Adrian Holovaty authored September 25, 2005

Showing 1 changed file with 10 additions and 0 deletions. Show diff stats Hide diff stats

  1. 10  django/core/meta/__init__.py
10  django/core/meta/__init__.py
@@ -592,6 +592,10 @@ def __new__(cls, name, bases, attrs):
592 592
             new_mod.get_latest = curry(function_get_latest, opts, new_class, does_not_exist_exception)
593 593
 
594 594
         for f in opts.fields:
  595
+            if f.choices:
  596
+                # Add "get_thingie_display" method to get human-readable value.
  597
+                func = curry(method_get_display_value, f)
  598
+                setattr(new_class, 'get_%s_display' % f.name, func)
595 599
             if isinstance(f, DateField) or isinstance(f, DateTimeField):
596 600
                 # Add "get_next_by_thingie" and "get_previous_by_thingie" methods
597 601
                 # 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)
990 994
     kwargs['limit'] = 1
991 995
     return get_object_func(**kwargs)
992 996
 
  997
+# CHOICE-RELATED METHODS ###################
  998
+
  999
+def method_get_display_value(field, self):
  1000
+    value = getattr(self, field.column)
  1001
+    return dict(field.choices).get(value, value)
  1002
+
993 1003
 # FILE-RELATED METHODS #####################
994 1004
 
995 1005
 def method_get_file_filename(field, self):

0 notes on commit 68c0742

Please sign in to comment.
Something went wrong with that request. Please try again.