Sam
Sam

Reputation: 501

Changing properties of inherited field

I want to alter properties of a model field inherited from a base class. The way I try this below does not seem to have any effect. Any ideas?

def __init__(self, *args, **kwargs):
    super(SomeModel, self).__init__(*args, **kwargs)
    f = self._meta.get_field('some_field')
    f.blank = True
    f.help_text = 'This is optional'

Upvotes: 2

Views: 1327

Answers (2)

Djangonaut
Djangonaut

Reputation: 5821

So.. You need to change blank and help_text attributes.. And I assume that you want this feature just so the help_text is displayed in forms, and form does not raise "this field is required"

So do this in forms:

class MyForm(ModelForm):
   class Meta:
      model = YourModel

   some_field = forms.CharField(required=False, help_text="Whatever you want")

Upvotes: 3

gruszczy
gruszczy

Reputation: 42168

OK, that's simply not possible, here is why:

http://docs.djangoproject.com/en/1.1/topics/db/models/#field-name-hiding-is-not-permitted

EDIT: And by the way: don't try to change class properties inside a constructor, it's not a wise thing to do. Basically what you are trying to do, is to change the table, when you are creating a row. You wouldn't do that, if you were just using SQL, would you :)? Completely different thing is changing forms that way - I often dynamically change instance a form, but then I still change only this one instance, not the whole template (a class) of form to be used (for example to dynamically add a field, that is required in this instance of a form).

Upvotes: 0

Related Questions