Reputation: 214
I am having an issue that I have database updated with the same value as for both id's even though they are different as per my example below: Lets assume that I entered as input for id 1 -- P_350 =" try_1" id 2 -- P_350 = "try_2" when I click submit then it become both id's the same "try_2" value. What you suggest to overcome this situation.
I have the below view.py for my database updates:
ids = request.POST.getlist("id")
qs = fp.objects.filter(id__in=ids)
P_350 = request.POST["P_350"]
P_450 = request.POST["P_450"]
updates = {}
if len(P_350) > 1:
updates['P_350'] = P_350
if len(P_450) > 1:
updates['P_450'] = P_450
if updates:
qs.update(**updates)
Here is my html file input section:
<td style="display:none;">
<input name="id" type="text" value={{ field.id }} >
</td>
<td width="650">
{{ field.FP_Item }}
</td>
{% if field.P_350|length == 0 %}
<td style="display:none;">
{% else %}
<td>
{% endif %}
<input name="P_350" type="text" value={{ field.P_350 }} >
</td>
{% if field.P_450|length == 0 %}
<td style="display:none;">
{% else %}
<td>
{% endif %}
<input name="P_450" type="text" value={{ field.P_450 }} >
</td>
Upvotes: 0
Views: 52
Reputation: 2223
Wrong logic
If you putting different value at same input P_350 this will get the last value you place when it reaches the backend, you probabily should put your first value in P_350 and the second in P_450.
In your example you only manipulate P_350 2 times... so P_450 will never be updated because he dont have a new value.
If you have multiple inputs related with P_350, you should first get all first and then send to backend (AJAX like async) or place multiple P_350 with different names and handle it at backend when save each one
https://medium.com/@taranjeet/adding-forms-dynamically-to-a-django-formset-375f1090c2b0
Upvotes: 1