On Sep 2, 6:10 pm, cArkraus <[EMAIL PROTECTED]> wrote: > Hey everyone, > > am pretty new to Django and Python in general. > I seem not be able to let the 'initial' value of a form-field be > calculated by its parent form. > > I'd like to do sth like this: > > class AbstractForm(forms.Form): > some_attr = "foo" > > def some_method(self): > if self.some_attr == "foo": return "foo" > return "Something else..." > > some_field = forms.CharField(initial=self.some_method()) > > ..but obviously I cant access 'self' when declaring some_field.. and > obviously I have yet to learn a lot about Python oop :) > > Fyi. I do not want to initiate form defaults like > f = MyForm(initial=...) in my views since I'd have to repeat that in > each view. > > I'd rather do things like this lateron: > class ConcreteForm(AbstractForm): > some_attr = "bar" > > Somebody willing to enlighten me? > > Thx a lot & cheers > carsten
I'm not sure I really understand what you want to do here, but I think the best thing would be for you to override the __init__() method. class AbstractForm(forms.Form): def __init__(self, *args, **kwargs): super(AbstractForm, self).__init__(*args, **kwargs) self.fields['some_field'].initial = self.some_method() If you need an __init__ in the child form as well, don't forget to call super(ConcreteForm, self).__init__() again there. -- DR. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---