Django 表单将初始数据添加到绑定表单
Django form add initial data to bound form
我正在尝试将第一种形式的初始数据添加到第二种形式中,但似乎无法正常工作。
我尝试了不同的方法,但我得到的最接近的方法来自 this post
这是我的代码
if request.method == 'POST':
laptop_select_form = LaptopSelectForm(request.POST)
if laptop_select_form.is_valid():
laptop_model_id = literal_eval(laptop_select_form.cleaned_data['model'])[0]
laptop_for_parts_form = LaptopForPartsForm(request.POST, laptop_id=laptop_model_id)
if laptop_for_parts_form.is_valid():
else:
laptop_select_form = LaptopSelectForm()
laptop_for_parts_form = LaptopForPartsForm()
和表格
class LaptopForPartsForm(forms.ModelForm):
def __init__(self, laptop_id=0, *args, **kwargs):
super(LaptopForPartsForm, self).__init__(*args, **kwargs)
self.laptop_id = laptop_id
class Meta:
model = LaptopForParts
fields = ('__all__')
widgets={'laptop_id': forms.HiddenInput()}
但是我一直收到这个错误:
laptop_for_parts_form = LaptopForPartsForm(request.POST, laptop_id=laptop_model_id)
TypeError: __init__() got multiple values for argument 'laptop_id'
有经验的人能给我指出正确的方向吗?
我更改了 request.POST 数据并在表单验证之前添加了 laptop_id
if request.method == 'POST':
laptop_select_form = LaptopSelectForm(request.POST)
if laptop_select_form.is_valid():
# Get laptop model ID from laptop_select_form
laptop_model_id = literal_eval(laptop_select_form.cleaned_data['model'])[0]
# Change immutable request.POST form data to add model ID
_mutable = request.POST.copy()
_mutable['laptop_id'] = laptop_model_id
laptop_for_parts_form = LaptopForPartsForm(_mutable)
if laptop_for_parts_form.is_valid():
laptop_for_parts_form.save()
else:
laptop_select_form = LaptopSelectForm()
laptop_for_parts_form = LaptopForPartsForm()
我正在尝试将第一种形式的初始数据添加到第二种形式中,但似乎无法正常工作。 我尝试了不同的方法,但我得到的最接近的方法来自 this post
这是我的代码
if request.method == 'POST':
laptop_select_form = LaptopSelectForm(request.POST)
if laptop_select_form.is_valid():
laptop_model_id = literal_eval(laptop_select_form.cleaned_data['model'])[0]
laptop_for_parts_form = LaptopForPartsForm(request.POST, laptop_id=laptop_model_id)
if laptop_for_parts_form.is_valid():
else:
laptop_select_form = LaptopSelectForm()
laptop_for_parts_form = LaptopForPartsForm()
和表格
class LaptopForPartsForm(forms.ModelForm):
def __init__(self, laptop_id=0, *args, **kwargs):
super(LaptopForPartsForm, self).__init__(*args, **kwargs)
self.laptop_id = laptop_id
class Meta:
model = LaptopForParts
fields = ('__all__')
widgets={'laptop_id': forms.HiddenInput()}
但是我一直收到这个错误:
laptop_for_parts_form = LaptopForPartsForm(request.POST, laptop_id=laptop_model_id)
TypeError: __init__() got multiple values for argument 'laptop_id'
有经验的人能给我指出正确的方向吗?
我更改了 request.POST 数据并在表单验证之前添加了 laptop_id
if request.method == 'POST':
laptop_select_form = LaptopSelectForm(request.POST)
if laptop_select_form.is_valid():
# Get laptop model ID from laptop_select_form
laptop_model_id = literal_eval(laptop_select_form.cleaned_data['model'])[0]
# Change immutable request.POST form data to add model ID
_mutable = request.POST.copy()
_mutable['laptop_id'] = laptop_model_id
laptop_for_parts_form = LaptopForPartsForm(_mutable)
if laptop_for_parts_form.is_valid():
laptop_for_parts_form.save()
else:
laptop_select_form = LaptopSelectForm()
laptop_for_parts_form = LaptopForPartsForm()