Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. class UserProfile(models.Model):
  2. user = models.OneToOneField(User, related_name="profile")
  3. #more fields
  4.  
  5. @staticmethod
  6. @receiver(post_save, sender=User, dispatch_uid="at_user_post_save")
  7. def user_post_save(sender, instance, **kwargs):
  8. #create new profile on user.save() (if necessary)
  9. profile, new = UserProfile.objects.get_or_create(user=instance)
  10.  
  11.  
  12. #in view
  13. profile = request.user.profile
  14.  
  15. AUTH_USER_MODEL = 'APPNAME.Account'
  16.  
  17. from django.db import models
  18. from django.contrib.auth.models import UserManager
  19. from django.contrib.auth.models import AbstractUser
  20.  
  21. class AccountManager(UserManager):
  22. def create_user(self, email, password=None, **kwargs):
  23. if not email:
  24. raise ValueError('Users must have a valid email address.')
  25. if not kwargs.get('username'):
  26. raise ValueError('Users must have a valid username.')
  27.  
  28. account = self.model(
  29. email=self.normalize_email(email),
  30. username=kwargs.get('username'),
  31. year_of_birth = kwargs.get('year_of_birth'),
  32. #MODEL = kwargs.get('MODEL_NAME'),
  33. )
  34. account.set_password(password)
  35. account.save()
  36.  
  37. return account
  38.  
  39. def create_superuser(self, email, password, **kwargs):
  40. account = self.create_user(email, password, **kwargs)
  41. account.is_staff = True
  42. account.is_superuser = True
  43. account.save()
  44.  
  45. return account
  46.  
  47. class Account(AbstractUser):
  48. email = models.EmailField(unique=True)
  49. #ADD YOUR MODELS HERE
  50.  
  51. objects = AccountManager()
  52.  
  53. def __str__(self):
  54. return self.email
  55.  
  56. from django.contrib import admin
  57. from django.contrib.auth.admin import UserAdmin
  58.  
  59. from .models import Account
  60.  
  61. @admin.register(Account)
  62. class UserAdmin(UserAdmin):
  63. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement