Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class UserProfile(models.Model):
- user = models.OneToOneField(User, related_name="profile")
- #more fields
- @staticmethod
- @receiver(post_save, sender=User, dispatch_uid="at_user_post_save")
- def user_post_save(sender, instance, **kwargs):
- #create new profile on user.save() (if necessary)
- profile, new = UserProfile.objects.get_or_create(user=instance)
- #in view
- profile = request.user.profile
- AUTH_USER_MODEL = 'APPNAME.Account'
- from django.db import models
- from django.contrib.auth.models import UserManager
- from django.contrib.auth.models import AbstractUser
- class AccountManager(UserManager):
- def create_user(self, email, password=None, **kwargs):
- if not email:
- raise ValueError('Users must have a valid email address.')
- if not kwargs.get('username'):
- raise ValueError('Users must have a valid username.')
- account = self.model(
- email=self.normalize_email(email),
- username=kwargs.get('username'),
- year_of_birth = kwargs.get('year_of_birth'),
- #MODEL = kwargs.get('MODEL_NAME'),
- )
- account.set_password(password)
- account.save()
- return account
- def create_superuser(self, email, password, **kwargs):
- account = self.create_user(email, password, **kwargs)
- account.is_staff = True
- account.is_superuser = True
- account.save()
- return account
- class Account(AbstractUser):
- email = models.EmailField(unique=True)
- #ADD YOUR MODELS HERE
- objects = AccountManager()
- def __str__(self):
- return self.email
- from django.contrib import admin
- from django.contrib.auth.admin import UserAdmin
- from .models import Account
- @admin.register(Account)
- class UserAdmin(UserAdmin):
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement