Django Custom User Admin Login
I'm creating a custom user by extending the AbstractBaseUser class. I have read the Django documentation and several tutorials online and I think that what I've done till now is very basic and should work.
The problem I'm facing is that even if I create a new DB from scratch and do a syncdb, the manage.py console **does not ask me for a admin username and password** (as it usually does). Hence, I cannot access /admin.
The problem is partially (and wrongly) resolved my using AUTH_PROFILE_MODULE instead of AUTH_USER_MODEL. This however leads to 'Users' and 'MyUser' being showed as different objects on the admin page i.e. MyUser is not set as the default user. (obviously)
**models.py**
class UserManager(BaseUserManager):
def create_user(self, username, email, corp, password=None):
if not (username or email or corp):
raise ValueError('Users must have an username, email and corporation')
user = self.model(username=username, email=UserManager.normalize_email(email), corp=corp, )
user.set_password(password)
user.is_active = True
user.save(using=self._db)
return user
def create_superuser(self, username, email, corp, password):
user = self.create_user(self, username, email, corp,
password=password, )
user.is_active = True
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=254, unique=True, blank=False)
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
email = models.EmailField(blank=True)
corp = models.ForeignKey(Client, related_name="is_employee_of")
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
def get_full_name(self):
return self.first_name + " " + self.last_name
def get_short_name(self):
return self.first_name
def __unicode__(self):
return self.username + "@" + self.corp.shortName
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email', 'corp']
**admin.py**
admin.site.register(Client)
admin.site.register(Address)
class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password Confirmation', widget=forms.PasswordInput)
class Meta:
model = MyUser
fields = ('username', 'email', 'corp', 'first_name', 'last_name')
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don'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
class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField()
class Meta:
model = MyUser
def clean_password(self):
return self.initial["password"]
class MyUserAdmin(UserAdmin):
form = UserChangeForm
add_form = UserCreationForm
list_display = ('username', 'email', 'corp', 'first_name', 'last_name', 'is_admin', 'is_staff')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('username', 'email', 'password')}),
('Personal Info', {'fields': ('first_name', 'last_name', 'corp')}),
('Permissions', {'fields': ('is_admin', 'is_staff')}),
('Important Dates', {'fields': ('last_login',)}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'password1', 'password2')}
),
)
search_fields = ('username',)
ordering = ('username',)
filter_horizontal = ()
admin.site.register(MyUser, MyUserAdmin)
**settings.py**
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'Profiles',
)
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
AUTH_USER_MODEL = "Profiles.MyUser"
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9a7ecb27-f4c7-4658-add2-db53049f06f0%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home