Save value from html tabel ofinputs

Hi, I’m new to coding and Django. I have a question. In my form I have the table with decimal values (6x6).

What is the best option to request and store in database all this value with possibility of modification.
At this moment I request all value by the name of input field. Next I collect the data in dict and after I saving to db JSONField.
For this moment this solution is working, but I’m not sure if data will be posted always in correct order.

def addrckikreport(request):
    form = rckikreportForm()

    if request.method == 'POST':

        wbc = request.POST.getlist('wbc')
        wbckeys = ['wbc1', 'wbc2', 'wbc3', 'wbc4', 'wbc5', 'wbc6',]
        wbcs = {wbckeys[i]: wbc[i] for i in range(len(wbckeys))}

        rbc = request.POST.getlist('rbc')
        rbckeys = ['rbc1', 'rbc2', 'rbc3', 'rbc4', 'rbc5', 'rbc6',]
        rbcs = {rbckeys[i]: rbc[i] for i in range(len(rbckeys))}

        hgb = request.POST.getlist('hgb')
        hgbkeys = ['hgb1', 'hgb2', 'hgb3', 'hgb4', 'hgb5', 'hgb6',]
        hgbs = {hgbkeys[i]: hgb[i] for i in range(len(hgbkeys))}

        hct = request.POST.getlist('hct')
        hctkeys = ['hct1', 'hct2', 'hct3', 'hct4', 'hct5', 'hct6',]
        hcts = {hctkeys[i]: hct[i] for i in range(len(hctkeys))}

        mcv = request.POST.getlist('mcv')
        mcvkeys = ['mcv1', 'mcv2', 'mcv3', 'mcv4', 'mcv5', 'mcv6',]
        mcvs = {mcvkeys[i]: mcv[i] for i in range(len(mcvkeys))}

        plt = request.POST.getlist('plt')
        pltkeys = ['plt1', 'plt2', 'plt3', 'plt4', 'plt5', 'plt6',]
        plts = {pltkeys[i]: plt[i] for i in range(len(pltkeys))}
        
        form = rckikreportForm(request.POST)

        if form.is_valid():
            report = form.save(commit=False)
            report.name = f"{'PKOAH'} {report.clientnumber} {report.date} {report.device} {report.serialnumber} {report.initials}"
            report.wbc = wbcs
            report.rbc = rbcs
            report.hgb = hgbs
            report.hct = hcts
            report.mcv = mcvs
            report.plt = plts
            report.save()
            return redirect('rckikreport')

    qcrange = range(1, 7)
    context = {'form':form, 'qcrange':qcrange}
    return render(request, 'rckik/report_form.html', context) 
class rckikreport(models.Model):
    name = models.CharField(max_length=200, blank=True, default='null')

    clientnumber = models.IntegerField(null=True)
    clientname = models.CharField(max_length=200, null=True)
    clientaddr = models.CharField(max_length=200, null=True)
    clientpostcode = models.CharField(max_length=200, null=True)
    clientcity = models.CharField(max_length=200, null=True)
    
    initials = models.ForeignKey(employees, on_delete=models.CASCADE,)
    date = models.DateField(default=date.today)

    device = models.ForeignKey(devices, on_delete=models.CASCADE, default=0)
    serialnumber = models.IntegerField(null=True)
    internalbarcode = models.CharField(max_length=200, null=True)
    externalbarcode = models.CharField(max_length=200, null=True)

    controlname = models.CharField(max_length=200, null=True)

    wbc = models.JSONField(blank=True, default=dict)
    rbc = models.JSONField(blank=True, default=dict)
    hgb = models.JSONField(blank=True, default=dict)
    hct = models.JSONField(blank=True, default=dict)
    mcv = models.JSONField(blank=True, default=dict)
    plt = models.JSONField(blank=True, default=dict)


    adddate = models.DateTimeField(auto_now_add=True)
    updatedate = models.DateTimeField(auto_now=True)
                <table class="table">
                    <thead>
                      <tr>
                        <th scope="col"></th>
                        <th scope="col">WBC</th>
                        <th scope="col">RBC</th>
                        <th scope="col">HGB</th>
                        <th scope="col">HCT</th>
                        <th scope="col">MCV</th>
                        <th scope="col">PLT</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr>
                        {% for qcno in qcrange %}
                        <th>{{qcno}}</th>
                        <td><input class="form-control form-control-sm mb-1" id="wbc{{ qcno }}" name="wbc" placeholder="WBC No. {{qcno}}"></td>
                        <td><input class="form-control form-control-sm mb-1" id="rbc{{ qcno }}" name="rbc"></td>
                        <td><input class="form-control form-control-sm mb-1" id="hgb{{ qcno }}" name="hgb"></td>
                        <td><input class="form-control form-control-sm mb-1" id="hct{{ qcno }}" name="hct"></td>
                        <td><input class="form-control form-control-sm mb-1" id="mcv{{ qcno }}" name="mcv"></td>
                        <td><input class="form-control form-control-sm mb-1" id="plt{{ qcno }}" name="plt"></td>
                      </tr>
                      {% endfor %}
                    </tbody>
                  </table>

If each row is a form consisting of multiple fields, then you can manage a number of rows as a Django formset.

On the other hand, if you’re working with this like a type of “grid input” like a spreadsheet might be, then you probably want to look at one of the JavaScript-based grid editing tools.

1 Like

I will check the Formset. On the other hand if I change the name of the fields to: wbc1, wbc2 etc, is possible to request all of the field and collect to array or something similar and save to one db field?

The Formset is enough for me! Thanks for good idea!