Look at line 79-84 in django/newforms.py (the default __init__). The 
very first thing you should (usually) do in __init__ is init your parent 
form class so that it sets up this current instance of the Form 
properly. You can then add (or remove) any dynamic fields you want to in 
self.fields. You'll also find your life easier if you don't try to 
replicate your parent class's args (data, files, etc) and just use a 
line like
def __init__(self, *args, **kwargs)
   super(self.__class__, self).__init__(*args, **kwargs)
so your class doesn't have to worry about them at all.

Eric Montgomery wrote:
> I am using newforms-admin to add custom validation for a Calendar
> app.  I am trying to handle recurring events by displaying extra
> fields in the EventForm when creating a new event, but then disabling
> those fields for already-existing events.  I overrode ModelForm's
> __init__ to achieve this by adding a "disabled" attribute to the
> widget for those fields so that they would be disabled when displayed
> as HTML.
> Here's the code:
> 
> class EventForm(forms.ModelForm):
> 
> ... extra field definitions here ...
> 
> def __init__(self, data=None, files=None, auto_id='id_%s',
> prefix=None,
>                  initial=None, error_class=ErrorList,
> label_suffix=':',
>                  empty_permitted=False, instance=None):
>         if instance is not Null:
>             self.recurring.widget.attrs = {'disabled': 'disabled'}
>             self.repeat_type.widget.attrs = {'disabled': 'true'}
>             self.repeat_interval.widget.attrs = {'disabled': 'true'}
>             self.end_on.widget.attrs = {'disabled': 'true'}
>             self.weekly_day_select.widget.attrs = {'disabled': 'true'}
>             self.monthly_repeat_type.widget.attrs = {'disabled':
> 'true'}
>             self.monthly_week_select.widget.attrs = {'disabled':
> 'true'}
>             self.monthly_day_select.widget.attrs = {'disabled':
> 'true'}
> 
>         super(EventForm, self).__init__(data, files, auto_id, prefix,
> initial, error_class,
>                                         label_suffix, empty_permitted,
> instance)
> 
> So, those 'disabled' attributes should only be set if the EventForm's
> instance is not Null.  That way, we don't allow the user to try to add
> recurring information to an existing Event, but the fields are
> available when creating a new Event.
> 
> However, it seems that if I go view one of the already-created Events,
> and my code in __init__ is executed and disables those fields, then
> from that point on, EventForm will leave those fields disabled, even
> if I try to add a new Event (at which point the instance *is* Null, so
> the fields should not get disabled).
> I have inserted print statements to check the flow of execution within
> __init__ and everything works as expected, but it seems like once
> those disabled attributes are set, they are set for good.
> 
> Maybe I don't understand Python well enough and by setting these
> values in __init__, I'm actually doing more than I think.
> 
> Thanks,
> Eric
> > 

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to