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