3models py from django db import models from django contrib auth model

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
###########3models.py
from django.db import models
from django.contrib.auth.models import User, UserManager
class UserProfile(User):
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
drugs = models.CharField(max_length=1, choices=DRUGS_CHOICES)
birthday = models.DateField()
country = models.ForeignKey(Country)
city = models.ForeignKey(City)
education = models.CharField(max_length=1, choices=EDUCATION_CHOICES, blank=True)
occupation = models.TextField(blank=True)
how_did_you_hear_about_us = models.TextField(blank=True)
how_you_can_help = models.TextField(blank=True)
about_me = models.TextField(blank=True)
objects = UserManager()
####################### admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from acc.models import UserProfile
class UserProfileAdmin(UserAdmin):
list_display = ('username', 'last_name', 'first_name',
'is_staff', 'is_active')
admin.site.unregister(User)
admin.site.register(UserProfile, UserProfileAdmin)
############################# auth_backend.py
from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ImproperlyConfigured
from django.db.models import get_model
import traceback
from acc.models import UserProfile
class UserProfileModelBackend(ModelBackend):
def authenticate(self, username=None, password=None):
try:
user = self.user_class.objects.get(username=username)
if user.check_password(password):
return user
except:
print traceback.print_exc()
return None
def get_user(self, user_id):
try:
return self.user_class.objects.get(pk=user_id)
except self.user_class.DoesNotExist:
return None
@property
def user_class(self):
if not hasattr(self, '_user_class'):
self._user_class = get_model(*settings.CUSTOM_USER_MODEL.split('.', 2))
if not self._user_class:
raise ImproperlyConfigured('Could not get custom user model')
return self._user_class
####################### settings.py
AUTHENTICATION_BACKENDS = (
'acc.auth_backends.UserProfileModelBackend',
)
CUSTOM_USER_MODEL = 'acc.UserProfile'