2

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

无法理解为什么它是错误的以及如何以正确的方式做?姜戈 1.8

4

1 回答 1

2

user.user_profile在创建之前您无法访问。你可以这样做:

user = User.objects.create_user(username=username, password=password)
userprofile = UserProfile.objects.create(user=user, fb_id=fb_id)
于 2017-01-19T12:21:40.720 回答