Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you look in django.contrib.auth admin.py, you'll see that the UserAdmin class specifies the add_form as UserCreationForm.</p> <p>UserCreationForm only includes the 'username' field from the User model.</p> <p>Since you're providing your own UserAdmin, you can just override the add_form to a custom UserCreationForm that includes the fields you need to make your signal work properly.</p> <p>Hope that helps you out.</p> <p>[Edit]</p> <p>Here's the UserCreationForm from contrib.auth forms.py:</p> <pre><code>class UserCreationForm(forms.ModelForm): """ A form that creates a user, with no privileges, from the given username and password. """ username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$', help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."), error_messages = {'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")}) password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput) password2 = forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput, help_text = _("Enter the same password as above, for verification.")) class Meta: model = User fields = ("username",) def clean_username(self): username = self.cleaned_data["username"] try: User.objects.get(username=username) except User.DoesNotExist: return username raise forms.ValidationError(_("A user with that username already exists.")) def clean_password2(self): password1 = self.cleaned_data.get("password1", "") password2 = self.cleaned_data["password2"] if password1 != password2: raise forms.ValidationError(_("The two password fields didn't match.")) return password2 def save(self, commit=True): user = super(UserCreationForm, self).save(commit=False) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user </code></pre> <p>Notice the fields = ("username",) tuple which excludes all other fields on the User model. You need something like:</p> <pre><code>class MyUserCreationForm(UserCreationForm): class Meta: model = User fields = ('username', 'email',) </code></pre> <p>then you can use that as the add_form in your custom UserAdmin:</p> <pre><code>class UserAdmin(admin.ModelAdmin): add_form = MyUserCreationForm </code></pre> <p>It's pretty late in my part of the world, but I'll see if I can get a working sample for you tomorrow.</p> <p>[Edit]</p> <p>Ok, here's the necessary changes you'll need to make to make this work. I've tested it using Django 1.3:</p> <pre><code>from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.admin import UserAdmin from django.contrib.auth.models import User from django import forms admin.site.unregister(User) class MyUserAdmin(UserAdmin): add_form = MyUserCreationForm add_fieldsets = ( (None, { 'classes': ('wide',), 'fields': ('username', 'email', 'password1', 'password2')} ), ) admin.site.register(User, MyUserAdmin) </code></pre> <p>I didn't see that the UserAdmin had an add_fieldset property initially. That's why the email field wasn't displaying in the add form.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload