Django:使用 inlineformset_factory 更新和创建数据记录
Django: Update and Create data record with inlineformset_factory
有人可以帮我吗我正在尝试使用“inlineformset_factory”我实际上让它工作但不是更新记录它只是复制记录
这是我的view.py
def resume(request):
user = request.user.personaldetails
form1 = personalDetailsForm(instance=user)
workExpFormSet = inlineformset_factory(
personalDetails, employmentHistory, form=employmentHistoryForm, extra=3, max_num=3)
formset = workExpFormSet(instance=user)
if request.method == 'POST':
form1 = personalDetailsForm(request.POST, request.FILES, instance=user)
formset = workExpFormSet(request.POST, instance=user)
if form1.is_valid():
form1.save()
if formset.is_valid():
formset.save()
else:
print(formset.errors)
# print(formset)
return render(request, 'main/Client/resume.html', {'form1': form1, 'formset': formset})
我的models.py
class personalDetailsForm(ModelForm):
profile_image = forms.ImageField(label="", widget=forms.FileInput(
attrs={'type': 'file', 'id': 'mediaFile'}))
jobTitle = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'jobTitle'}))
fname = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'fname'}))
lname = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'lname'}))
email = forms.EmailField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'email', 'class': 'form-control', 'id': 'email'}))
phone = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'phone'}))
country = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'country'}))
city = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'city'}))
address = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'address'}))
birthplace = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'pdate'}))
birthdate = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'bdate'}))
nationality = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'national'}))
profSummary = forms.CharField(label="", required=False, widget=forms.Textarea(
attrs={'type': 'text', 'class': 'form-control', 'id': 'summaryEditor'}))
postalcode = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'postal'}))
class Meta:
model = personalDetails
fields = '__all__'
exclude = ['accounts']
class employmentHistoryForm(ModelForm):
JobTitle = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control jobTitle', 'name': 'jobTitle'}))
start_date = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control startDate', 'name': 'start_date'}))
end_date = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control endDate', 'name': 'end_date'}))
employer = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control employerName', 'name': 'employer'}))
city = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control employerCity', 'name': 'city'}))
description = forms.CharField(label="", required=False, widget=forms.Textarea(
attrs={'type': 'text', 'class': 'form-control employeeEditor', 'style': 'height: 100px;', 'name': 'description'}))
class Meta:
model = employmentHistory
fields = '__all__'
exclude = ['personal']
最后是用于自定义模型的表格
class employmentHistoryForm(ModelForm):
JobTitle = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control jobTitle', 'name': 'jobTitle'}))
start_date = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control startDate', 'name': 'start_date'}))
end_date = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control endDate', 'name': 'end_date'}))
employer = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control employerName', 'name': 'employer'}))
city = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control employerCity', 'name': 'city'}))
description = forms.CharField(label="", required=False, widget=forms.Textarea(
attrs={'type': 'text', 'class': 'form-control employeeEditor', 'style': 'height: 100px;', 'name': 'description'}))
class Meta:
model = employmentHistory
fields = '__all__'
exclude = ['personal']
每当我 enter/save 我的表单数据时,它都会创建一条记录并添加另一条记录,该记录与前一条记录中的数据相同,它只是乘以最后输入的数据...但是每当我试图改变最后一条输出的数据有错误:
[{'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}]
这是因为您没有在您的模板中包含 id 字段,或者可能实施错误(因为您没有包含您的模板代码,我假设这就是问题所在,很可能是这种情况)。由于 Forms 没有找到与 post 数据关联的任何 ID,因此它假定这是一个创建请求而不是编辑请求。
有人可以帮我吗我正在尝试使用“inlineformset_factory”我实际上让它工作但不是更新记录它只是复制记录
这是我的view.py
def resume(request):
user = request.user.personaldetails
form1 = personalDetailsForm(instance=user)
workExpFormSet = inlineformset_factory(
personalDetails, employmentHistory, form=employmentHistoryForm, extra=3, max_num=3)
formset = workExpFormSet(instance=user)
if request.method == 'POST':
form1 = personalDetailsForm(request.POST, request.FILES, instance=user)
formset = workExpFormSet(request.POST, instance=user)
if form1.is_valid():
form1.save()
if formset.is_valid():
formset.save()
else:
print(formset.errors)
# print(formset)
return render(request, 'main/Client/resume.html', {'form1': form1, 'formset': formset})
我的models.py
class personalDetailsForm(ModelForm):
profile_image = forms.ImageField(label="", widget=forms.FileInput(
attrs={'type': 'file', 'id': 'mediaFile'}))
jobTitle = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'jobTitle'}))
fname = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'fname'}))
lname = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'lname'}))
email = forms.EmailField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'email', 'class': 'form-control', 'id': 'email'}))
phone = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'phone'}))
country = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'country'}))
city = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'city'}))
address = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'address'}))
birthplace = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'pdate'}))
birthdate = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'bdate'}))
nationality = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'national'}))
profSummary = forms.CharField(label="", required=False, widget=forms.Textarea(
attrs={'type': 'text', 'class': 'form-control', 'id': 'summaryEditor'}))
postalcode = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control', 'id': 'postal'}))
class Meta:
model = personalDetails
fields = '__all__'
exclude = ['accounts']
class employmentHistoryForm(ModelForm):
JobTitle = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control jobTitle', 'name': 'jobTitle'}))
start_date = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control startDate', 'name': 'start_date'}))
end_date = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control endDate', 'name': 'end_date'}))
employer = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control employerName', 'name': 'employer'}))
city = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control employerCity', 'name': 'city'}))
description = forms.CharField(label="", required=False, widget=forms.Textarea(
attrs={'type': 'text', 'class': 'form-control employeeEditor', 'style': 'height: 100px;', 'name': 'description'}))
class Meta:
model = employmentHistory
fields = '__all__'
exclude = ['personal']
最后是用于自定义模型的表格
class employmentHistoryForm(ModelForm):
JobTitle = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control jobTitle', 'name': 'jobTitle'}))
start_date = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control startDate', 'name': 'start_date'}))
end_date = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control endDate', 'name': 'end_date'}))
employer = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control employerName', 'name': 'employer'}))
city = forms.CharField(label="", required=False, widget=forms.TextInput(
attrs={'type': 'text', 'class': 'form-control employerCity', 'name': 'city'}))
description = forms.CharField(label="", required=False, widget=forms.Textarea(
attrs={'type': 'text', 'class': 'form-control employeeEditor', 'style': 'height: 100px;', 'name': 'description'}))
class Meta:
model = employmentHistory
fields = '__all__'
exclude = ['personal']
每当我 enter/save 我的表单数据时,它都会创建一条记录并添加另一条记录,该记录与前一条记录中的数据相同,它只是乘以最后输入的数据...但是每当我试图改变最后一条输出的数据有错误:
[{'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}]
这是因为您没有在您的模板中包含 id 字段,或者可能实施错误(因为您没有包含您的模板代码,我假设这就是问题所在,很可能是这种情况)。由于 Forms 没有找到与 post 数据关联的任何 ID,因此它假定这是一个创建请求而不是编辑请求。