The name of the fields will be the same name you use in your model.

You can replace the loop with explicit calls for each field::

    self.fields['name'].widget.attrs['class'] = yourClass1
    self.fields['username'].widget.attrs['class'] = yourClass2

Or if you want to pass a list in as an argument just pop() it before
calling super()::

    class BaseYourForm(forms.BaseForm):
        def __init__(self, *args, **kwargs):
            tmpVariable = kwargs.pop('yourListofFields', False)

            super(BaseYourForm, self).__init__(*args, **kwargs)

            for i in tmpVariable:
                self.fields[i].widget.attrs['class'] =
'yourclassname'

You were close by passing your list of fields to the base form you
created, but you actually need to pass it when you instantiate your
form::

    YourForm = form_for_model(YourModel, form=BaseYourForm)
    list_of_required = ['name', 'username']
    form = YourForm(yourlistoffields=list_of_required)

Cheers.
- whiteinge


On Jun 24, 2:46 pm, l5x <[EMAIL PROTECTED]> wrote:
> And what if I need a class only in selected fields ?:)
> Can I pass it somehow through a list of the fields ?
>
> Something like that should be nice:
>
> list_of_required = ['name', 'username']
> MyForm = form_for_model(MyModel, form=
> BaseYourForm(list_of_required))
>
> Hm. I guess that I only have to modify the loop:
>
>             for i in self.fields:
>                 self.fields[i].widget.attrs['class'] =
> 'yourclassname'
>
> But what's the name of field ? i.name ? or what ?
>
> On Jun 24, 10:31 pm, l5x <[EMAIL PROTECTED]> wrote:
>
> > I think that it's a right thing for me. Thanks for your effort.


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