views:

107

answers:

2

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'
A: 

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).

gruszczy
It seems that abstract = True is exactly what I was looking for. However, in your example, I think you meant to sublass A. class B(A)Which leads to exactly the same error.
Sam
Yeah, this is what I meant to do. It raises an exception? I will take a look at it, when I get access to my dev machine.
gruszczy
I have added a link, that should interest you.
gruszczy
+2  A: 

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")
Pydev UA