Login

Modify properties on fields inherited from an abstract superclass

Author:
andybak
Posted:
June 29, 2014
Language:
Python
Version:
Not specified
Tags:
model field modelfield help_text verbose_name
Score:
0 (after 0 ratings)

See http://stackoverflow.com/questions/927729/how-to-override-the-verbose-name-of-a-superclass-model-field-in-django/24475838#24475838

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def modify_fields(**kwargs):
    """
    Allows you to modify certain properties on fields inherited from an abstract superclass
    Usage:
    @modify_fields(timestamp={
        'verbose_name': 'Available From',
        'help_text': 'Earliest date you can book this'})
    class Purchase(BaseOrderItem):
        pass
    """
    def wrap(cls):
        for field, prop_dict in kwargs.items():
            for prop, val in prop_dict.items():
                setattr(cls._meta.get_field(field), prop, val)
        return cls
    return wrap

More like this

Comments

ffsffd (on June 4, 2015):
<p>Neat approach, but this doesn't appear to work on django 1.7+ anymore - attrs on initialized fields seem to have been made readonly and you end up with AttributeError: can't set attribute</p>

#

Please login first before commenting.