Ticket #468: django-choices-accessor.patch

File django-choices-accessor.patch, 1.2 KB (added by robert@…, 10 years ago)

patch

  • django/core/meta/__init__.py

     
    592592            new_mod.get_latest = curry(function_get_latest, opts, new_class, does_not_exist_exception)
    593593
    594594        for f in opts.fields:
     595            if len(f.choices) != 0:
     596                # Add an accessor method to get to the human readable value
     597                func = curry(method_get_display_value, f)
     598                setattr(new_class, 'get_%s_display' % f.name , func)
    595599            if isinstance(f, DateField) or isinstance(f, DateTimeField):
    596600                # Add "get_next_by_thingie" and "get_previous_by_thingie" methods
    597601                # for all DateFields and DateTimeFields that cannot be null.
     
    991995    kwargs['limit'] = 1
    992996    return get_object_func(**kwargs)
    993997
     998# CHOICE-RELATED METHODS ###################
     999
     1000def method_get_display_value(field, self):
     1001    value = getattr(self, field.name)
     1002    for (v, d) in field.choices:
     1003        if v==value:
     1004            return d
     1005    # Couldn't find it in the list
     1006    return value
     1007
    9941008# FILE-RELATED METHODS #####################
    9951009
    9961010def method_get_file_filename(field, self):
Back to Top