Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # models.py
- from django.contrib.auth.models import BaseUserManager
- from django.contrib.auth.models import AbstractBaseUser
- from django.contrib.auth.models import PermissionsMixin
- from django.utils.translation import ugettext_lazy as _
- class UserManager(BaseUserManager):
- def _create_user(self, email, password, **extra_fields):
- """
- Creates and saves a User with the given email and password.
- """
- if not email:
- raise ValueError("Users must have an email address")
- email = self.normalize_email(email)
- user = self.model(email = email, **extra_fields)
- user.set_password(password)
- user.save()
- return user
- def create_superuser(self, email, password, **extra_fields):
- extra_fields.setdefault('is_staff', True)
- extra_fields.setdefault('is_superuser', True)
- extra_fields.setdefault('is_active', True)
- if extra_fields.get('is_staff') is not True:
- raise ValueError('Superuser must have is_staff=True.')
- if extra_fields.get('is_superuser') is not True:
- raise ValueError('Superuser must have is_superuser=True.')
- return self._create_user(email, password, **extra_fields)
- class User(AbstractBaseUser, PermissionsMixin):
- # I had add the username field despite that I don't use in my User model
- username = models.CharField(_('username'), max_length=30, null=True,
- help_text=_('Required. 30 characters or fewer. Letters, digits and ''@/./+/-/_ only.'),
- validators=[RegexValidator(r'^[\w.@+-]+$', _('Enter a valid username.'), 'invalid')
- ])
- email = models.EmailField(unique=True, null=True,
- help_text=_('Required. Letters, digits and ''@/./+/-/_ only.'),
- validators=[RegexValidator(r'^[\w.@+-]+$', _('Enter a valid email address.'), 'invalid')
- ])
- is_staff = models.BooleanField(
- _('staff status'),
- default=False,
- help_text=_('Designates whether the user can log into this site.'),
- )
- is_active = models.BooleanField(
- _('active'),
- default=True,
- help_text=_(
- 'Designates whether this user should be treated as active. '
- 'Unselect this instead of deleting accounts.'
- ),
- )
- objects = UserManager()
- USERNAME_FIELD = "email"
- class Meta:
- db_table = 'auth_user'
- verbose_name_plural = 'Usuarios en la plataforma'
- def __str__(self):
- return "@{}".format(self.email)
- # forms.py
- rom django.contrib.auth import get_user_model
- from django.contrib.auth.forms import UserChangeForm, UserCreationForm
- from django import forms
- class CustomUserChangeForm(UserChangeForm):
- class Meta(UserChangeForm.Meta):
- model = get_user_model()
- class CustomUserCreationForm(UserCreationForm):
- class Meta(UserCreationForm.Meta):
- model = get_user_model()
- # I specify the email field to the create user
- fields = ('email',)
- class UserCreateForm(UserCreationForm):
- class Meta:
- fields = ("email", "password1", "password2",)
- model = get_user_model()
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.fields["email"].label = "Email address"
- # admin.py
- from __future__ import unicode_literals
- from django.contrib import admin
- from django.contrib.auth.admin import UserAdmin
- from .models import User
- from .forms import CustomUserChangeForm, CustomUserCreationForm
- # Inherit of the original UserAdmin for use the customized forms
- class CustomUserAdmin(UserAdmin):
- form = CustomUserChangeForm
- add_form = CustomUserCreationForm
- fieldsets = UserAdmin.fieldsets + (
- (
- None, {
- 'classes':('wide',),
- 'fields':(
- 'slug',
- #'first_name',
- #'last_name',
- 'display_name',
- 'gender',
- 'country_of_origin',
- 'city_of_origin',
- 'country_current_residence',
- 'city_current_residence',
- 'speak_languages',
- #'email',
- 'phone_number',
- 'address',
- 'bio',
- 'avatar',
- 'date_of_birth',
- 'is_student',
- 'is_professor',
- 'is_executive',
- 'is_study_host',
- 'is_innovation_host',
- 'is_hosting_host',
- 'is_entertainment_host',
- 'is_other_services_host',
- ),
- }
- ),
- )
- # exclude = ('first_name', 'last_name')
- # Change our UserAdmin class to inherit of our CustomUserAdmin created above (do not inherit of model.ModelAdmin)
- @admin.register(User)
- class UserAdmin(CustomUserAdmin):
- list_display = ('id',
- 'email',
- 'slug',
- 'first_name',
- 'last_name',
- 'gender',
- 'country_of_origin',
- 'phone_number',
- 'address',
- 'bio',
- 'date_of_birth',
- 'is_student',
- 'is_professor',
- 'is_executive',
- 'is_study_host',
- "is_innovation_host",
- "is_hosting_host",
- "is_entertainment_host",
- "is_other_services_host",
- )
- # settings.py
- AUTH_USER_MODEL = ‘my_app_name.User’
Advertisement
Add Comment
Please, Sign In to add comment