Creating a form that updates only 1 field of an entry

Viewed 33

I am currently trying to create a checklist that would print a customer database. this checklist would print all the customers for a specific service - in this case, its IT14's- and it would let the user update the checklist field (isdone_IT14)

However, the error I am running into at the moment, is that the form is not valid due to the fact that the rest of the fields are required.

It looks like when the checklist is ticked, the form is trying to create a new entry.

How would I fix that issue so that it only updates the field for that one entry?

Please see the below code and errors (print(form.errors))

Models.py:

class CompanyClass(models.Model):
    #Company Only Fields
    CompanyName = models.CharField(max_length=50 , blank=False)
    RefNo = models.CharField(max_length=50 , blank=False )

    #Contact Details
    ContactPerson = models.CharField( max_length=50, blank=False)
    EmailAddress = models.CharField(max_length=50, blank=False)

    #Services
    IT14 = models.BooleanField(default=True)
    FinancialStatements = models.BooleanField(default=True)
    IsProvisionalTaxPayer = models.BooleanField(default=True)
    AnnualReturns = models.BooleanField(default=True)

    # CheckList
    isdone_IT14 = models.BooleanField(default=False)
    isdone_FinancialStatements = models.BooleanField(default=False)
    isdone_Provisionals = models.BooleanField(default=False)
    isdone_AnnualReturns = models.BooleanField(default=False)

Views.py:

class companyIT14(View):

    def get(self,request):
        it14s = CompanyClass.objects.filter(IT14 = True).order_by('CompanyName')
        form = CompanyForm()
        content = {'it14s':it14s , 'form':form}
        return render(request, 'main/Checklists/companyIT14.html', content)

    def post(self,request):
        it14s = CompanyClass.objects.filter(IT14=True).order_by('CompanyName')
        form = CompanyForm(request.POST)
        content = {'it14s': it14s, 'form': form}
        if form.is_valid():
            form.save()
            return redirect('companyIT14')
        else:
            print(form.errors)
            return render(request, 'main/checklistHome.html', content)

Template:

{% extends "main/base.html"%}
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
{% block content %}
<body>
<br>
    {% if user.is_authenticated %}
    <h1 class="h1" style="text-align: center">Companies IT14's</h1>

<table class="table table-hover table-dark"  style="width: 80%;text-align: center;">
    <thead>
        <tr>
            <th scope="col">Company</th>
            <th scope="col">Ref No.</th>
            <th scope="col">Contact Person</th>
            <th scope="col">E-mail Address</th>
            <th scope="col">Completed?</th>
            <th scope="col">Save</th>
        </tr>
    </thead>
    <tbody>
    {% for x in it14s %}
        <tr>
            <th scope="row">{{ x.CompanyName }}</th>
            <th>{{ x.RefNo }}</th>
            <th>{{ x.ContactPerson }}</th>
            <th>{{ x.EmailAddress }}</th>
            <form class="form-group mt-4" method="post">
            {% csrf_token %}
            <th>{{ form.isdone_IT14  }}</th>
            <th><button class="btn btn-success" type="submit">Save</button> </th>
            </form>
    {% endfor %}
</table>

    {% endif %}
</body>
{% endblock %}
</html>

Errors

<ul class="errorlist"><li>CompanyName<ul class="errorlist"><li>This field is required.</li></ul></li><li>RefNo<ul class="errorlist"><li>This field is required.</li></ul></li><li>FinancialYearEnd<ul class="errorlist"><li>This field is r equired.</li></ul></li><li>ProvisionalTaxDate1<ul class="errorlist"><li>This field is required.</li></ul></li><li>ProvisionalTaxDate2<ul class="errorlist"><li>This field is required.</li></ul></li><li>ARMonth<ul class="errorlist"><li>T his field is required.</li></ul></li><li>TaxRegNo<ul class="errorlist"><li>This field is required.</li></ul></li><li>StreetName<ul class="errorlist"><li>This field is required.</li></ul></li><li>StreetNo<ul class="errorlist"><li>This f ield is required.</li></ul></li><li>BuildingName<ul class="errorlist"><li>This field is required.</li></ul></li><li>BuildingNo<ul class="errorlist"><li>This field is required.</li></ul></li><li>Suburb<ul class="errorlist"><li>This fiel d is required.</li></ul></li><li>City<ul class="errorlist"><li>This field is required.</li></ul></li><li>ContactPerson<ul class="errorlist"><li>This field is required.</li></ul></li><li>OfficeNumber<ul class="errorlist"><li>This field is required.</li></ul></li><li>CellNumber<ul class="errorlist"><li>This field is required.</li></ul></li><li>EmailAddress<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
1 Answers

In your model you need to specify blank=True so that the form handling knows that those fields are not required. If you don't have a default field you would also want to set null=True so that the database can accept empty field values.

Here's a link to the official docs.

Also, if you are using class based views I would suggest using the UpdateView that Django supplies rather than the View class, as its is designed specifically for updating existing records, without you having to add much logic.

It may also be useful to share your CompanyForm, as this is where you'd house the logic to update only that specific field.

Related