我正在尝试通过扩展默认的 django 用户来创建 UserProfile 模式。无法保存附加值,因为用户没有用户配置文件: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 扩展 Django 身份验证模型 User,避免相关ObjectDoesNotExist 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41741597/