'Django form add initial data to bound form
I am trying to add initial data from 1 form into the second form but I cannot seem to get it work. I've tried different methods but the closest I got came from this post
Here is my code
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()
And the form
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()}
But I keep getting this error:
laptop_for_parts_form = LaptopForPartsForm(request.POST, laptop_id=laptop_model_id)
TypeError: __init__() got multiple values for argument 'laptop_id'
Can someone more experienced please point me in the right direction?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
