I'm using 0.96.  I define the following form

class TForm(forms.Form):
    admin = forms.ChoiceField()
    x = forms.IntegerField()

    def __init__(self, data=None, **kwargs):
        super(TForm, self).__init__(data, kwargs)
        admins = [(a.name, a.name) for a in Admin.objects.all()]
        self.fields['admin']._set_choices(admins)


Then, I instantiate it so:

f = TForm(initial={'x': 23})

If I dump out the table with as_table(), the initial x value is not in
the form.  If I comment out the __init__ "constructor", initial is
honored and I see the initial x value as specified.  I define an
__init__ for the purpose of setting choices on the admin ChoiceField.

I can initialize x manually in the __init__ function as follows:

    def __init__(self, data=None, **kwargs):
        super(TForm, self).__init__(data, kwargs)
        admins = [(a.name, a.name) for a in Admin.objects.all()]
        self.fields['admin']._set_choices(admins)
        if kwargs.has_key('initial'):
            vinit = kwargs['initial']
            if vinit.has_key('x'): self.fields['x'].initial =
vinit['x']

Can someone explain why initial is ignored when I define my own
__init__?  Is there a prescription for defining your own __init__ when
you sub-class Form and/or Model?  I'm no Python expert and definitely
a newb when it comes to its new-style classes, so please take it easy
on me.  :)


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