小编Eth*_*Jay的帖子

django.db.utils.IntegrityError:NOT NULL 约束失败:users_profile.user_id

我目前正在开发一个项目,该项目将接收用户信息并存储它。我的问题是我一直遇到这个 NOT NULL 约束因用户 ID 错误而失败。我相信这是因为当表单尝试保存时有一个空用户,但不知道我可以做什么来修复它。我尝试使用这一行:

form.user = Profile.objects.get(user=self.request.user)
Run Code Online (Sandbox Code Playgroud)

但它不起作用并给了我这个错误:

/users/pii/ 处的名称错误

名称“self”未定义

任何可以为我指明正确方向的帮助或建议将不胜感激!

模型.py

class Profile(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    gender = models.CharField(max_length = 1, choices = GENS, default = '')
    birthday = models.DateField(default = '1900-01-01')
    address = AddressField(on_delete=False, blank=True, null=True)
    race = models.CharField(max_length = 2, choices = RACES, default = 'x')
    ethnicity = models.CharField(max_length = 1, choices = ETHNICITIES, default = 'x')
    income = models.CharField(max_length = 1, choices = INCBRACKET, default = 'x')
    education …
Run Code Online (Sandbox Code Playgroud)

python django django-models django-forms

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

django ×1

django-forms ×1

django-models ×1

python ×1