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

  1. Getting dynamic model choices in newforms by ubernostrum 8 years, 1 month ago
  2. Extended i18n base model by alcinnz 2 years ago
  3. Clone model mixin by zakj 6 years, 3 months ago
  4. Change model field attribute in a DRY way by jedie 3 years, 6 months ago
  5. UUIDField by bwhittington 5 years, 11 months ago

Comments

Please login first before commenting.