several forms on the same page with django
<p>i have a very simple model... something like this:</p>

<pre><code>class MachineTransTable(models.Model):
    ...
    file = models.ForeignKey('File', related_name='the_file')
    source = models.TextField()
    target = models.TextField()
    ...
</code></pre>

<p>What I'd like to do is to have a page where the user has the source on the left (disable), the target on the right (editable) and a submit button to post the edited target text for EACH selected object in the MachineTransTable table. Here are some more information to better understand my request:</p>

<ul>
<li>A page refers to a single file and there are several (sometimes hundreds) of objects in the MachineTransTable table belonging to the same file</li>
<li>Each time the user edit a single target and hit the submit button for that object, the object is saved/updated (depending on the initial value of the object) in the DB and the the user can continue to edit all the other objects...</li>
<li>At the end of the page there is another submit button which is used to exit from the page at the end of the work (all the objects have been edited/updated). If an object has not been edited/updated, it keeps its original value.</li>
</ul>

<p>I tried to use a formset but I guess it's not the right choice... This is the file forms.py</p>

<pre><code>class SegmentForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(SegmentForm, self).__init__(*args, **kwargs)
        if self.instance.id:
            self.fields['source'].widget.attrs['readonly'] = True

    class Meta:
        model = MachineTransTable
        fields = ['source','target',]

SegmentFormSet = inlineformset_factory(File, MachineTransTable, form=SegmentForm, fields=('source','target'), extra=0)
</code></pre>

<p>and the view.py file is:</p>

<pre><code>class CatUpdateView(LoginRequiredMixin,UpdateView):

    Model = MachineTransTable
    context_object_name = 'file'
    template_name = 'app_cat/cat.html'
    form_class = SegmentForm
    ...
    def get_context_data(self, **kwargs):
        context = super(CatUpdateView, self).get_context_data(**kwargs)
        formset = SegmentFormSet(instance=self.get_object())
        context['formset_Segment'] = formset
        return context
</code></pre>

<p>Using this approach, I have a single form and all the related objects are saved/updated at once when the submit button is hit... </p>

<p>What can I do to achieve what I described above? Thanks</p>

<p>I think your choice is correct, you should not use  <strong>several (hundreds eventually) forms</strong> here if the field in the same model. For two reasons:</p>

<ul>
<li><p>You have to do a lot of repeat job to write so many forms and that is easy to make mistake and hard to maintain.</p></li>
<li><p>You still have to connect the database and update the record no matter how many field had been edit, and they almost efficiency.</p></li>
</ul>

<p>But if you really want to do this,you can just use Ajax to post the current parameters name to the api, then update it, For instance, you have a button for target field:</p>

<pre><code>&lt;a href="api/table_id" class="button target"&gt;value_in_the_html&lt;/a&gt;
</code></pre>

<p>Use Ajax to post the field name and value:</p>

<pre><code>$ajax({
    url: "api/table_id",
    type: "POST",
    datatype: "json",
    data: {"field": "target", "value": "value_in_the_html"}
});
</code></pre>

<p>In view.py:</p>

<pre><code>def UpdateTable(request, id):
    field = request.POST.get("field", None)
    value = request.POST.get("value", None)
    machine = MachineTransTable.objects.filter(id=id).first()
    machine.getattr(machine,field) = value
    machine.save()
    return HttpResponse("Saved!")
</code></pre>

python django django-forms formset