views:

208

answers:

2
class EditAdminForm(forms.ModelForm):
    password = username.CharField(widget=forms.TextInput())
    password = forms.CharField(widget=forms.PasswordInput())
    password_confirm = forms.CharField(widget=forms.PasswordInput(), initial=???)

You can see what I'm trying to do here. How would I go about pre-populating the pasword_confirm field (which is not part of the model). I'm so confused.

A: 

You can define

___init_
method in EditAdminForm.

something like:


class EditAdminForm(forms.ModelForm):
    username = forms.CharField(widget=forms.TextInput())
    password = forms.CharField(widget=forms.PasswordInput())
    def __init__(self, initial_from, data=None, initial=None)
        sefl.fields['password_confirm'] = forms.CharField(widget=forms.PasswordInput(), initial=initial_from)
Zada Zorg
Thank you for your answer too. I didn't try because I was a bit confused, but going forward I don't think I want that coupled with the form itself because then if I use the form without an instance, it'll throw an error.
orokusaki
+1  A: 

You can't access the instance in the form declaration, because there isn't one until you instantiate it.

However, if all you want to do is set dynamic initial data, do this with the initial parameter on instantation:

form = EditAdminForm(initial={'password':'abcdef'})
Daniel Roseman