Django:如何使用 inline_formset 创建一个与用户具有多对一关系的表单
Django: How to create a form using inline_formset with a many to one relationship with user
我正在尝试使用 Django 的 inlineformset_factory 设置一个表单。该模型是建立在多对一的关系中,每个用户都会有很多个案例。我希望用户添加案例。
我遵循了this site
的大纲
models.py
class Case(models.Model):
date_due = models.DateField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
forms.py
CaseFormset = inlineformset_factory(User,Case,
fields=('date_due',),
can_delete = True)
class CaseForm(forms.ModelForm):
class Meta:
model = Case
fields = ('date_due',)
views.py
@login_required
def add_case(request):
if request.method == 'POST':
form = CaseForm(request.POST)
if form.is_valid():
case = form.save(commit = False)
case_formset = CaseFormset(request.POST, instance = case)
if case_formset.is_valid():
case.save()
case_formset.save()
return redirect('index')
else:
print(form.errors)
else:
form = CaseForm()
case_formset = CaseFormset(instance = Case())
return render(request, 'cases/add_case.html',
{'form':form, 'case_formset':case_formset})
add_case.html
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<body>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
{{ case_formset.as_p }}
{{ case_formset.management_form }}
<button type="submit">Save changes</button>
</form>
</body>
</head>
</html>
我得到一个
Exception Type: ValidationError
Exception Value:
['ManagementForm data is missing or has been tampered with']
阅读文档看起来我需要将 management_form 添加到表单集中,我已经完成了。我不确定还有哪里出错了。
我也尝试了一种不同于the docs
的方法
它看起来与上面的略有不同,并尝试使用用户作为实例创建一个表单集。我不确定哪种方法更可取。
我有两个问题。
- 为什么会出现上述错误?
- 我应该将表单集设置为用户实例而不是大小写吗?
谢谢
我想通了!
在 views.py 我需要添加 case.user = request.user
所以views.py看起来像
@login_required
def add_case(request):
if request.method == 'POST':
form = CaseForm(request.POST)
if form.is_valid():
case = form.save(commit=False)
case.user = request.user
case_formset = CaseFormset(request.POST, instance = case)
if case_formset.is_valid():
case.save()
case_formset.save()
return redirect('index')
else:
print(form.errors)
else:
form = CaseForm()
case_formset = CaseFormset(instance = Case())
return render(request, 'cases/add_case.html',
{'form':form, 'case_formset':case_formset})
我正在尝试使用 Django 的 inlineformset_factory 设置一个表单。该模型是建立在多对一的关系中,每个用户都会有很多个案例。我希望用户添加案例。
我遵循了this site
的大纲models.py
class Case(models.Model):
date_due = models.DateField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
forms.py
CaseFormset = inlineformset_factory(User,Case,
fields=('date_due',),
can_delete = True)
class CaseForm(forms.ModelForm):
class Meta:
model = Case
fields = ('date_due',)
views.py
@login_required
def add_case(request):
if request.method == 'POST':
form = CaseForm(request.POST)
if form.is_valid():
case = form.save(commit = False)
case_formset = CaseFormset(request.POST, instance = case)
if case_formset.is_valid():
case.save()
case_formset.save()
return redirect('index')
else:
print(form.errors)
else:
form = CaseForm()
case_formset = CaseFormset(instance = Case())
return render(request, 'cases/add_case.html',
{'form':form, 'case_formset':case_formset})
add_case.html
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<body>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
{{ case_formset.as_p }}
{{ case_formset.management_form }}
<button type="submit">Save changes</button>
</form>
</body>
</head>
</html>
我得到一个
Exception Type: ValidationError
Exception Value:
['ManagementForm data is missing or has been tampered with']
阅读文档看起来我需要将 management_form 添加到表单集中,我已经完成了。我不确定还有哪里出错了。
我也尝试了一种不同于the docs
的方法它看起来与上面的略有不同,并尝试使用用户作为实例创建一个表单集。我不确定哪种方法更可取。
我有两个问题。
- 为什么会出现上述错误?
- 我应该将表单集设置为用户实例而不是大小写吗?
谢谢
我想通了!
在 views.py 我需要添加 case.user = request.user
所以views.py看起来像
@login_required
def add_case(request):
if request.method == 'POST':
form = CaseForm(request.POST)
if form.is_valid():
case = form.save(commit=False)
case.user = request.user
case_formset = CaseFormset(request.POST, instance = case)
if case_formset.is_valid():
case.save()
case_formset.save()
return redirect('index')
else:
print(form.errors)
else:
form = CaseForm()
case_formset = CaseFormset(instance = Case())
return render(request, 'cases/add_case.html',
{'form':form, 'case_formset':case_formset})