Skip to content

Commit

Permalink
Fixed #468 -- Model classes now get an accessor method to get the hum…
Browse files Browse the repository at this point in the history
…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
adrianholovaty committed Sep 25, 2005
1 parent 272eab5 commit 68c0742
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions django/core/meta/__init__.py
Expand Up @@ -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) new_mod.get_latest = curry(function_get_latest, opts, new_class, does_not_exist_exception)


for f in opts.fields: 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): if isinstance(f, DateField) or isinstance(f, DateTimeField):
# Add "get_next_by_thingie" and "get_previous_by_thingie" methods # Add "get_next_by_thingie" and "get_previous_by_thingie" methods
# for all DateFields and DateTimeFields that cannot be null. # for all DateFields and DateTimeFields that cannot be null.
Expand Down Expand Up @@ -990,6 +994,12 @@ def method_get_next_or_previous(get_object_func, field, is_next, self, **kwargs)
kwargs['limit'] = 1 kwargs['limit'] = 1
return get_object_func(**kwargs) 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 ##################### # FILE-RELATED METHODS #####################


def method_get_file_filename(field, self): def method_get_file_filename(field, self):
Expand Down

0 comments on commit 68c0742

Please sign in to comment.