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
This commit is contained in:
parent
272eab5cd8
commit
68c0742008
|
@ -592,6 +592,10 @@ class ModelBase(type):
|
||||||
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.
|
||||||
|
@ -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):
|
||||||
|
|
Loading…
Reference in New Issue