I have an HTML view of a ModelForm where I'm trying to take a separate user input called scanner_input
, run some code on it, and then replace request.post['product_id']
with the result from the code I just ran.
Everywhere I look, it says the same thing, that request.post objects can be made mutable by using .copy() and yet I get the following error message:
'SerialInstanceForm' object does not support item assignment
The error occurs in this line from my view:
post['product_id'] = scanner_input_list[i]
Here's my view:
def SerialMulti(request):
if request.method == "POST":
form = SerialInstanceForm(request.POST)
if form.is_valid():
scanner_input = request.POST['scanner_input']
scanner_input_list = scanner_input.splitlines()
for i in range(0, len(scanner_input_list)):
post = SerialInstanceForm(request.POST.copy())
post['product_id'] = scanner_input_list[i]
post.save()
return render(request, 'serial_multi.html', {'form': form})
else:
form = SerialInstanceForm()
return render(request, 'serial_multi.html', {'form': form})
My model:
class ProductSerialInstance(models.Model):
STATUS_CHOICES = (
('in_inventory', 'In Stock'),
('given_out', 'Given Out'),
('repair', 'Repair')
)
name = models.ForeignKey(ProductSerial)
employee = models.ForeignKey(Employee, blank=True, null=True)
it_dep = models.ForeignKey(ItDep)
product_id = models.CharField(max_length=50)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.name
And my form:
class SerialInstanceForm(forms.ModelForm):
# duplications = forms.IntegerField()
class Meta:
model = ProductSerialInstance
fields = ['name','employee','it_dep','status']