简体   繁体   English

Django模型向导:IntegrityError列“ user_id”不能为空

[英]Django Model Wizard : IntegrityError Column “user_id” cannot be null

I get a slight problem when trying to use django wizard and I am scratching my head to solve it 尝试使用django向导时出现一个小问题,我挠头解决了问题

Here is my model: 这是我的模型:

class Profile(models.Model):
    profile_user = models.OneToOneField(User,db_index=True)
    studio = models.ForeignKey(Account)

And here is my wizard.py: 这是我的wizard.py:

from django.utils.translation import ugettext_lazy as _
from django.forms.models import modelform_factory
from formtools.wizard.views import SessionWizardView
from .models import *

class NewModelWizard(SessionWizardView):
     form_list = [
         (_('New model registration'),modelform_factory(User, fields=('username','password'))),
         (_('Model data'), modelform_factory(Profile))
     ]
template_name = "create_profile.html"

def done(self,form_list, **kwargs):
    user = form_list[0].save()
    profile = form_list[1].save(commit=False)
    profile.studio = self.request.user.account
    profile.user = User.objects.get(id=user.id)
    profile.save()

After submitting the wizard, I encounter the following error: 提交向导后,我遇到以下错误:

IntegrityError : (1048, "Column 'profile_user_id' cannot be null")

I don't see what I am doing wrong here, does anybody get a clue about what is happening and how to fix it ? 我在这里看不到我在做什么错,是否有人知道发生了什么以及如何解决?

Your field name is profile_user , so use that. 您的字段名称是profile_user ,所以使用它。

profile.profile_user = user

Note you don't have to refetch the user from the db, just use user . 请注意,您不必从数据库中重新获取用户,只需使用user

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

相关问题 IntegrityError-列“ user_id”不能为null - IntegrityError - Column 'user_id' cannot be null /auth/userreg/ 处的 IntegrityError(1048,“列 'user_id' 不能为空”) - IntegrityError at /auth/userreg/ (1048, "Column 'user_id' cannot be null") Django:IntegrityError:列user_id不是唯一的 - Django: IntegrityError: column user_id is not unique Django'列'user_id'不能为空' - Django 'Column 'user_id' cannot be null' 注释 model 仅适用于用户 django(1048,“列 'user_id' 不能为空”或没有用户填写) - Comment model only for user django (1048, “Column 'user_id' cannot be null” or no user filled) Django:(1048,“列'user_id'不能为null”) - Django: (1048, “Column 'user_id' cannot be null”) 提交表单时,为什么Django在“ user_id”列中引发IntegrityError:null值违反了非null约束? - Why does Django raised IntegrityError :null value in column “user_id” violates not-null constraint when a form is committed? 1048, "列 'user_id' 不能为空" - 1048, "Column 'user_id' cannot be null" IntegrityError:错误:“ user_id”列中的空值违反了非空约束 - IntegrityError: ERROR: null value in column “user_id” violates not-null constraint Django auth.models用户一对多(1048,“列'user_id'不能为空”) - Django auth.models User One-To-Many (1048, “Column 'user_id' cannot be null”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM