views:

1243

answers:

4

Is there a good way to do this in django without rolling my own authentication system? I want the username to be the user's email address instead of them creating a username.

Please advise, thank you.

+8  A: 

What we did. It isn't a "complete" solution, but it does much of what you're looking for.

class UserForm( forms.ModelForm ):
    class Meta:
        model= User
        exclude= ('email',)
    username = forms.EmailField(max_length=64,
        help_text = "The person's email address.")
    def clean_email( self ):
        email= self.cleaned_data['username']
        return email

class UserAdmin( admin.ModelAdmin ):
    form= UserForm
    list_display = ( 'email', 'first_name', 'last_name', 'is_staff' )
    list_filter = ( 'is_staff', )
    search_fields = ( 'email', )

admin.site.unregister( User )
admin.site.register( User, UserAdmin )
S.Lott
Helpful. Thanks.
Paolo Bergantino
Works for me. Although I can see this being confusing for future maintainers.
nbolton
+1  A: 

you can also find an interesting discussion on this topic at the below link :

http://groups.google.com/group/django-users/browse_thread/thread/c943ede66e6807c/2fbf2afeade397eb#2fbf2afeade397eb

Rama Vadakattu
+2  A: 

S.Lott's solution has an error in the subclassing of UserAdmin which breaks changing passwords from the admin. Here's a fixed version. I've also added the imports needed.

from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin 
from django.contrib.auth.models import User

class UserForm(forms.ModelForm):
    class Meta:
        model = User
        exclude = ('email',)
    username = forms.EmailField(max_length=64,
                                help_text="The person's email address.")
    def clean_email(self):
        email = self.cleaned_data['username']
        return email

class UserAdmin(UserAdmin):
    form = UserForm
    list_display = ('email', 'first_name', 'last_name', 'is_staff')
    list_filter = ('is_staff',)
    search_fields = ('email',)

admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Dan
Comment to answer, don't fork it.
Török Gábor