Last active
December 22, 2015 00:18
-
-
Save arulmr/6387963 to your computer and use it in GitHub Desktop.
Django 1.5 - Custom User Models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from django.contrib.auth.models import Group | |
from django.contrib.auth.admin import UserAdmin | |
class UserProfileAdmin(UserAdmin): | |
form = UserChangeForm | |
add_form = UserCreationForm | |
list_display = ('email', 'mobile', 'is_superuser') | |
list_filter = ('is_superuser',) | |
fieldsets = ( | |
(None, {'fields': ('email', 'password')}), | |
('Personal info', {'fields': ('mobile',)}), | |
('Permissions', {'fields': ('is_superuser', 'user_permissions', )}), | |
('Important dates', {'fields': ('last_login',)}), | |
) | |
add_fieldsets = ( | |
(None, { | |
'classes' : ('wide',), | |
'fields' : ('email', 'mobile', 'password1', 'password2')} | |
), | |
) | |
search_fields = ('email',) | |
ordering = ('email',) | |
filter_horizontal = () | |
admin.site.register(UserProfile, UserProfileAdmin) | |
admin.site.unregister(Group) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from django import forms | |
from django.contrib.auth.forms import ReadOnlyPasswordHashField | |
from custom_auth.models import UserProfile | |
class UserCreationForm(forms.ModelForm): | |
password1 = forms.CharField(label='Password', widget=forms.PasswordInput) | |
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) | |
class Meta: | |
model = UserProfile | |
fields = ('email', 'mobile') | |
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 does not match") | |
return password2 | |
def clean_mobile(self): | |
mobile = self.cleaned_data.get('mobile') | |
if not re.match(r'^\d{10}$', mobile): | |
raise forms.ValidationError('Enter a valid mobile number') | |
return mobile | |
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 = UserProfile | |
def clean_password(self): | |
return self.initial["password"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from django.contrib.auth import models as auth_models | |
class CustomUserManager(auth_models.BaseUserManager): | |
def create_user(self, email, mobile, password): | |
user = self.model( | |
email = CustomUserManager.normalize_email(email), | |
mobile = mobile, | |
) | |
user.is_staff = True | |
user.set_password(password) | |
user.save(using = self._db) | |
return user | |
def create_superuser(self, email, mobile, password): | |
print "ValueError" | |
if not re.match(r'^\d{10}$', mobile): | |
raise ValueError('Enter a valid mobile number') | |
user = self.model( | |
email = CustomUserManager.normalize_email(email), | |
mobile = mobile, | |
) | |
user.is_staff = True | |
user.is_superuser = True | |
user.set_password(password) | |
user.save(using = self._db) | |
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.contrib.auth import models as auth_models | |
class UserProfile(auth_models.AbstractBaseUser, auth_models.PermissionsMixin): | |
email = models.EmailField(verbose_name = 'E-Mail', unique = True) | |
mobile = models.CharField(verbose_name = 'Mobile Number', max_length = 10, unique = True) | |
is_staff = models.BooleanField() | |
USERNAME_FIELD = 'email' | |
REQUIRED_FIELDS = ['mobile', ] | |
objects = CustomUserManager() | |
def get_full_name(self): | |
return self.email | |
def get_short_name(self): | |
return self.email | |
def __unicode__(self): | |
return self.email | |
def is_staff(self): | |
return self.is_staff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserProfile(auth_models.AbstractBaseUser, auth_models.PermissionsMixin): | |
... | |
USERNAME_FIELD = 'email' | |
REQUIRED_FIELDS = ['mobile', ] | |
objects = CustomUserManager() | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.contrib import admin | |
from django.contrib.auth import get_user_model | |
class DummyModel(models.Model): | |
user_key = models.ForeignKey(get_user_model()) | |
admin.site.register(DummyModel) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment