Django所有Auth文檔都不錯。您可以擴展您的用戶配置文件是這樣的:
models.py
class StudentProfile(models.Model):
userId = models.OneToOneField(User, on_delete=models.CASCADE, related_name='student_profile')
user_dp = models.ImageField(upload_to = upload_location, null = True, blank=True, width_field="width_field", height_field="height_field")
height_field = models.IntegerField(default=0, null=True, blank=True)
width_field = models.IntegerField(default=0, null=True, blank=True)
first_name = models.CharField(max_length=120, blank=True)
last_name = models.CharField(max_length=120, blank=True)
gender_choices = (
('Unspecified', 'Unspecified'),
('Male', 'Male'),
('Female', 'Female'),
('Other', 'Other'),
)
gender = models.CharField(max_length=20, choices=gender_choices, default='Unspecified')
college_name = models.CharField(max_length=500, blank=True)
date_of_birth = models.DateField(blank=True, null=True)
graduation_year = models.IntegerField(blank=True, null=True)
description = models.TextField(blank = True, null=True)
created_on = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_on = models.DateTimeField(auto_now=True, auto_now_add=False)
onboarded = models.BooleanField(default=False)
address = models.ForeignKey(Address, blank=True, null=True)
def __str__(self):
return self.first_name + self.last_name
這是使用用戶(Django的內置AUTH)作爲onetoone場學生簡介。我一直在我的項目中使用它。如果你想要社交登錄,建議使用所有身份驗證。您可以使用預保存信號/在用戶註冊時擴展默認註冊以創建用戶配置文件。像這樣:
class SocialAccountAdapter(DefaultSocialAccountAdapter):
def save_user(self, request, sociallogin, form=None):
user = super(SocialAccountAdapter, self).save_user(request, sociallogin, form)
changes = 0
try:
profile = StudentProfile.objects.get(userId=user).first()
except StudentProfile.DoesNotExist:
profile = StudentProfile()
profile.userId = user
profile.save()
if not profile.user_dp:
url = sociallogin.account.get_avatar_url()
avatar = download_file_from_url(url)
if avatar:
profile.user_dp = avatar
changes = 1
if changes == 1:
profile.save()
return user
def pre_social_login(self, request, sociallogin):
"""
Invoked just after a user successfully authenticates via a
social provider, but before the login is actually processed
(and before the pre_social_login signal is emitted).
We're trying to solve different use cases:
- social account already exists, just go on
- social account has no email or email is unknown, just go on
- social account's email exists, link social account to existing user
"""
print (sociallogin.account.extra_data)
# Ignore existing social accounts, just do this stuff for new ones
if sociallogin.is_existing:
return
# some social logins don't have an email address, e.g. facebook accounts
# with mobile numbers only, but allauth takes care of this case so just
# ignore it
if 'email' not in sociallogin.account.extra_data:
if 'emailAddress' not in sociallogin.account.extra_data:
return
else:
# Note: __iexact is used to ignore cases
try:
email = sociallogin.account.extra_data['emailAddress'].lower()
user = User.objects.get(email=email)
# if it does not, let allauth take care of this new social account
except User.DoesNotExist:
sp = StudentProfile()
return
# if it does, connect this new social login to the existing user
sociallogin.connect(request, user)
# check if given email address already exists.
# Note: __iexact is used to ignore cases
else :
try:
email = sociallogin.account.extra_data['email'].lower()
user = User.objects.get(email=email)
# if it does not, let allauth take care of this new social account
except User.DoesNotExist:
sp = StudentProfile()
return
# if it does, connect this new social login to the existing user
sociallogin.connect(request, user)
希望這會有所幫助。
https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html –
我會從[官方文檔]開始(https://docs.djangoproject .COM/EN /開發/主題/認證/定製/#延長-的現有用戶模型)。由於您只想添加額外的字段並且不會更改認證行爲,所以最好的辦法是使用「配置文件方法」。 – Selcuk