diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2005-09-25 20:36:05 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2005-09-25 20:36:05 +0000 |
| commit | 68c074200807a967b71aa798d7908d0b9c06b3c7 (patch) | |
| tree | 8a40642e1bb2d251d3ef6e84374de8bddadcf9ad | |
| parent | 272eab5cd8ebafc68af3bca65f4f0d217c49ce53 (diff) | |
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
| -rw-r--r-- | django/core/meta/__init__.py | 10 |
1 files changed, 10 insertions, 0 deletions
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): |
