用户没有用户个人资料:RelatedObjectDoesNotExist

时间:2017-01-19 12:16:24

标签: django

我正在尝试通过扩展默认的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

1 个答案:

答案 0 :(得分:1)

在创建user.user_profile之前,您无法访问user = User.objects.create_user(username=username, password=password) userprofile = UserProfile.objects.create(user=user, fb_id=fb_id) 。你可以这样做:

{{1}}