简体   繁体   English

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

[英]django.db.utils.IntegrityError: NOT NULL constraint failed: users_profile.user_id

I am currently working on a project that would take in a users information and store it.我目前正在从事一个项目,该项目将接收用户信息并存储它。 My problem is I keep running into this NOT NULL constraint failed with the user id error.我的问题是我一直遇到这个 NOT NULL 约束因用户 ID 错误而失败。 I believe this comes from having a null user when the form is trying to save, but don't know what I could do to fix it.我相信这是因为当表单试图保存时有一个空用户,但不知道我能做些什么来修复它。 I tried using this line:我尝试使用这一行:

form.user = Profile.objects.get(user=self.request.user)

but it didn't work and gave me this error:但它不起作用并给了我这个错误:

NameError at /users/pii/ /users/pii/ 处的名称错误

name 'self' is not defined名称“自我”未定义

Any help or advice that would point me in the right direction would be greatly appreciated!任何可以为我指明正确方向的帮助或建议将不胜感激!

models.py模型.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 = models.CharField(max_length = 2, choices = EDUCATION, default = 'x')
    employment = models.CharField(max_length = 1, choices = EMPLOYMENT, default = 'x')

    def __str__(self):
        return f'{self.user.username} Profile'
    def save(self, *args, **kawrgs):
        super().save(*args, **kawrgs)
        img = Image.open(self.image.path)
        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

views.py视图.py

def PII(request):
    if request.method == 'POST':
        form = PIIForm(request.POST,)
        if form.is_valid():
            form.save()
            messages.success(request, f'Your account has been created! You are now able to log in')
            return redirect('finalpii')
    else:
        form = PIIForm(request.POST)
    return render(request, 'users/pii.html', {'form':form})

forms.py表格.py

class PIIForm(forms.ModelForm):
    birthday = forms.DateField()
    class Meta:
        model = Profile
        fields = [
        'gender',
        'birthday',
        'address',
        'race',
        'ethnicity'
        ]

You have to edit your user field of Profile model as...您必须将Profile模型的user字段编辑为...

user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)

null=True , Django will store empty values as NULL in the database. null=True ,Django 将在数据库中将空值存储为 NULL。 Default is False.默认值为假。

blank=True , form validation will allow entry of an empty value. blank=True ,表单验证将允许输入空值。 Default is False.默认值为假。

Then run python manage.py makemigrations and python manage.py migrate commands and then you can add a profile with Null user.然后运行python manage.py makemigrationspython manage.py migrate命令,然后你可以添加一个带有Null用户的配置文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django.db.utils.IntegrityError: NOT NULL 约束失败:app_users.key_value_id - django.db.utils.IntegrityError: NOT NULL constraint failed: app_users.key_value_id django.db.utils.IntegrityError:唯一约束失败:core_profile.user_id - django.db.utils.IntegrityError: UNIQUE constraint failed: core_profile.user_id django.db.utils.IntegrityError: NOT NULL 约束失败:accounts_user.user_id - django.db.utils.IntegrityError: NOT NULL constraint failed: accounts_user.user_id django.db.utils.IntegrityError: NOT NULL 约束失败:user.id - django.db.utils.IntegrityError: NOT NULL constraint failed: user.id django.db.utils.IntegrityError:唯一约束失败:new__bank_profile.fname_id - django.db.utils.IntegrityError: UNIQUE constraint failed: new__bank_profile.fname_id Createsuperuser django.db.utils.IntegrityError: NOT NULL 约束失败 - Createsuperuser django.db.utils.IntegrityError: NOT NULL constraint failed django.db.utils.IntegrityError:NOT NULL约束失败 - django.db.utils.IntegrityError: NOT NULL constraint failed django.db.utils.IntegrityError: NOT NULL 约束失败: - django.db.utils.IntegrityError: NOT NULL constraint failed: django.db.utils.IntegrityError: NOT NULL 约束失败来自 Postman - django.db.utils.IntegrityError: NOT NULL constraint failed fom Postman django.db.utils.IntegrityError:NOT NULL 约束失败:new__users_personal_detail.husband_adhaarcopy - django.db.utils.IntegrityError: NOT NULL constraint failed: new__users_personal_detail.husband_adhaarcopy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM