如何使用 UserProfile 扩展 Django 身份验证模型用户,避免 RelatedObjectDoesNotExist 错误?
How to extend Django auth model User with UserProfile, avoiding RelatedObjectDoesNotExist error?
我正在尝试通过扩展默认的 django 用户来创建 UserProfile 模式。无法保存附加值,因为 User has no user profile:RelatedObjectDoesNotExist
:
user = User.objects.create_user(username=username, password=password)
user.save()
print(fb_id)
user.userprofile.fb_id = fb_id
user.userprofile.save()
模态:
class UserProfile(models.Model):
user = models.OneToOneField(User)
fb_id = models.BigIntegerField(null=True, blank=True, default=0)
follows = models.ManyToManyField('self', related_name='followed_by', symmetrical=False)
不明白为什么会出错,如何正确处理? Django 1.8
您必须先创建 user.user_profile
才能访问它。你可以这样做:
user = User.objects.create_user(username=username, password=password)
userprofile = UserProfile.objects.create(user=user, fb_id=fb_id)
我正在尝试通过扩展默认的 django 用户来创建 UserProfile 模式。无法保存附加值,因为 User has no user profile:RelatedObjectDoesNotExist
:
user = User.objects.create_user(username=username, password=password)
user.save()
print(fb_id)
user.userprofile.fb_id = fb_id
user.userprofile.save()
模态:
class UserProfile(models.Model):
user = models.OneToOneField(User)
fb_id = models.BigIntegerField(null=True, blank=True, default=0)
follows = models.ManyToManyField('self', related_name='followed_by', symmetrical=False)
不明白为什么会出错,如何正确处理? Django 1.8
您必须先创建 user.user_profile
才能访问它。你可以这样做:
user = User.objects.create_user(username=username, password=password)
userprofile = UserProfile.objects.create(user=user, fb_id=fb_id)