[Answer]-How to implement a form to update the quantity?

1👍

It looks like you’ll be interested in FormSets, specifically InlineFormSets:

https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-an-inline-formset-in-a-view

If you want to make it stupidly easy (possibly at the expense of understanding how it all works), you can use my generic views for exactly this use case.

My library: https://github.com/AndrewIngram/django-extra-views

Then…

views.py:

import extra_views 

class EditProductReviewsView(extra_views.InlineFormSetView):
    model = Garage
    inline_model = GarageCar
    fields = ('quantity', )
    template_name = 'garage.html'

garage.html:

<form action="." method="post">
{% csrf_token %}

{{ formset.management_form }}

<table>
{% for form in formset %}
<tr>
<td>{{ form.instance.car.name }}</td>
<td>{{ form.instance.car.price }}</td>
{% for field in form.visible_fields %}
<td>
{{ field }}
</td>
{% endfor %}
<div style="display: none;">
{% for field in form.hidden_fields %}{{ field }}{% endfor %}
</div>
<td>{{ form.quantity }}</td>
<td>{{ form.instance.car.id }}</td>
</tr>
{% endfor %}
</table>


<input type="submit" />
</form>

I’ve not tested this example, I can’t remember if you’ll need to add the id field to the fields tuple or not. But the basic gist should be right.

Leave a comment