提交表单时出现 Django IntegrityError(在视图中预填充)
Django IntegrityError When Submitting Form (pre-populating in views)
我无法为我的问题找到可靠的答案。我正在尝试在视图中预填充表单。因此,当用户使用表单时,一些数据已经得到处理。我想做的是自动获取用户用户名和他的组并将其输入数据库。但是当我尝试这样做时,我得到 IntegrityError NOT NULL constraint failed.
我是新手,正在从 MDN 网络文档中学习。
我的views.py:
class AddNewView(generic.View):
formClass = AddNewForm
template_name = 'myapp/subscription_form.html'
def get(self, request):
form = self.formClass(None)
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.formClass(request.POST)
if form.is_valid():
subscription = form.save(commit=False)
organization = request.user.groups.values_list('id', flat=True).first()
input_user = self.request.user
stuff = form.cleaned_data['stuff']
description = form.cleaned_data['description']
subscription.save()
return render(request, self.template_name, {'form': form})
forms.py:
class AddNewForm(forms.ModelForm):
def __init__(self, *args,**kwargs):
super (AddNewView, self ).__init__(*args,**kwargs)
class Meta:
model = Subscription
fields = [
'stuff',
'description',
]
models.py:
class Subscription(models.Model):
organization = models.ForeignKey(Group, help_text="which organization")
input_user = models.CharField(max_length=150)
input_date = models.DateTimeField(auto_now_add=True)
description = models.CharField(max_length=1000, null=True, blank=True, help_text='Description')
stuff = models.CharField(max_length=100)
html:
{% extends "base_generic.html" %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit" />
</form>
{% endblock %}
我得到的错误是:
异常类型:IntegrityError
异常值:
NOT NULL 约束失败:myapp_myapp.organization_id
感谢您提供的任何帮助
您实际上并未将值分配给您的订阅列
if form.is_valid():
subscription = form.save(commit=False)
subscription.organization = request.user.groups.first()
subscription.input_user = self.request.user
subscription.stuff = form.cleaned_data['stuff']
subscription.description = form.cleaned_data['description']
subscription.save()
在您的表单中,
class AddNewForm(forms.ModelForm):
class Meta:
model = Subscription
fields = [
'description',
]
尝试像这样编辑您的视图,
if form.is_valid():
subscription = form.save(commit=False)
subscription.organization = request.user.groups.first()
subscription.input_user = self.request.user.username
subscription.save()
我不太明白为什么你在字段选项中包含 stuff
,因为它不是你模型中的字段(至少不是你发布的模型中的字段)。
我无法为我的问题找到可靠的答案。我正在尝试在视图中预填充表单。因此,当用户使用表单时,一些数据已经得到处理。我想做的是自动获取用户用户名和他的组并将其输入数据库。但是当我尝试这样做时,我得到 IntegrityError NOT NULL constraint failed.
我是新手,正在从 MDN 网络文档中学习。
我的views.py:
class AddNewView(generic.View):
formClass = AddNewForm
template_name = 'myapp/subscription_form.html'
def get(self, request):
form = self.formClass(None)
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.formClass(request.POST)
if form.is_valid():
subscription = form.save(commit=False)
organization = request.user.groups.values_list('id', flat=True).first()
input_user = self.request.user
stuff = form.cleaned_data['stuff']
description = form.cleaned_data['description']
subscription.save()
return render(request, self.template_name, {'form': form})
forms.py:
class AddNewForm(forms.ModelForm):
def __init__(self, *args,**kwargs):
super (AddNewView, self ).__init__(*args,**kwargs)
class Meta:
model = Subscription
fields = [
'stuff',
'description',
]
models.py:
class Subscription(models.Model):
organization = models.ForeignKey(Group, help_text="which organization")
input_user = models.CharField(max_length=150)
input_date = models.DateTimeField(auto_now_add=True)
description = models.CharField(max_length=1000, null=True, blank=True, help_text='Description')
stuff = models.CharField(max_length=100)
html:
{% extends "base_generic.html" %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit" />
</form>
{% endblock %}
我得到的错误是:
异常类型:IntegrityError
异常值:
NOT NULL 约束失败:myapp_myapp.organization_id
感谢您提供的任何帮助
您实际上并未将值分配给您的订阅列
if form.is_valid():
subscription = form.save(commit=False)
subscription.organization = request.user.groups.first()
subscription.input_user = self.request.user
subscription.stuff = form.cleaned_data['stuff']
subscription.description = form.cleaned_data['description']
subscription.save()
在您的表单中,
class AddNewForm(forms.ModelForm):
class Meta:
model = Subscription
fields = [
'description',
]
尝试像这样编辑您的视图,
if form.is_valid():
subscription = form.save(commit=False)
subscription.organization = request.user.groups.first()
subscription.input_user = self.request.user.username
subscription.save()
我不太明白为什么你在字段选项中包含 stuff
,因为它不是你模型中的字段(至少不是你发布的模型中的字段)。