change a widget attribute in ModelForm without define the field

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from django import forms
from django.contrib.auth.models import User 

class UserForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)

        # change a widget attribute:
        self.fields['user_permissions'].widget.attrs["size"] = 5
         
    class Meta:
        model = User 

More like this

  1. Add model form field meta data in a DRY way by jedie 4 years, 2 months ago
  2. Add attrs automaticlly through ModelForm field max_length by lettoo 1 year, 4 months ago
  3. Reorder fields directly in the ModelForm by HM 5 years ago
  4. filtered ModelChoiceField queries by robharvey 5 years, 2 months ago
  5. DisplayModelForm by koenb 5 years ago

Comments

guettli (on September 17, 2008):

Related: Snippet 755

#

(Forgotten your password?)