Django Formsets with ModelBase Not rendering the Checkbox default but a drop down list instead

159 views Asked by At

I need to create a required checkbox option if no_new_item exists. I am using the model.NullBoolean field. According to Django docs the Boolean field should render the checkbox widget but NullBoolean renders Select. The reason for the switch to NullBoolean was due to the null error when migrating. So now I am getting a drop down list with 'Yes' and 'No.' How would I go about creating the checkbox in the Base model.Models with NullBoolean or is there a better way?

(this is an edit as I miss spoke about the Django Docs. Thanks @Alasdair)

/models.py

class StoreNightlyReport(models.Model):
    store_number = models.ForeignKey('Stores', verbose_name='Store Number', max_length=20, null=True)
    date = models.DateField(null=True)
    #managers = models.ManyToManyField('auth.User', blank=True, null=True)

    def __str__(self):
        return self.store_number.store_number


class StoreNightlyReportsBase(models.Model):
    store_nightly_report = models.ForeignKey(StoreNightlyReport)
    no_new_item = models.NullBooleanField(verbose_name='No New Items', default=True)
    customer = models.CharField(verbose_name='Customer Name', max_length=20, null=True)

    class Meta:
        abstract = True

/forms.py

class StoreNightlyReportsForm(ModelForm):
    class Meta:
        model = StoreNightlyReport
        widgets = {'date': SelectDateWidget()}
        exclude = ()

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user')
        super(StoreNightlyReportsForm, self).__init__(*args, **kwargs)
        if 'store_number' in self.fields:
            if not user.is_superuser:
                self.fields['store_number'].queryset = Stores.objects.filter(managers=user)

/views.py

class StoresNightlyReportsNewLoansCreate(CreateView):
    template_name = 'reports/storenightlyreport_form.html'
    model = StoreNightlyReport
    form_class = forms.StoreNightlyReportsForm
    success_url = reverse_lazy('reports:storenightlyreports')

    def get_form(self, form_class=None):
        form_class = self.get_form_class()

        form = form_class(self.request.POST or None, user=self.request.user)
        self.formsets = {}
        StoreNightlyReportsNewLoanFormSet = inlineformset_factory(StoreNightlyReport,
                                                                  StoreNightlyReportsNewLoan,
                                                                  exclude=[],
                                                                  extra=1)
        self.formsets['new_loan'] = StoreNightlyReportsNewLoanFormSet(self.request.POST or None)

    def get_context_data(self, **kwargs):
        data = super(StoresNightlyReportsNewLoansCreate, self).get_context_data(**kwargs)
        data['formset_new_loan'] = self.formsets['new_loan']
        return data


    def get_context_data(self, **kwargs):
        data = super(StoresNightlyReportsNewLoansCreate, self).get_context_data(**kwargs)
        #formset_renewal = StoreNightlyReportsRenewalFormSet()
        data['formset_new_loan'] = self.formsets['new_loan']
    return super(StoresNightlyReportsNewLoansCreate, self).form_invalid(form)

/template.html

{% extends "reports/base.html" %}
{% load static %}

{% block body_block %}


<div class="container-fluid">
    <form class="form-inline" action="" method="post">
        {% csrf_token %}
        {{ form }}

        <table class="table">
            {{ formset_new_loan.management_form }}
            <div>
            <br><h4><strong>New Loans</strong></h4><br>
            {% include "reports/formsets.html" with formset=formset_new_loan formset_class_name='new_loan' %}
            </div>
        </table>
1

There are 1 answers

0
Prof. Falken On BEST ANSWER

Fixed

After some thought I just went back to the model.Base and changed it to the BooleanField type and added a default value. This cleared the null error on migration and rendered a checkbox option.

class StoreNightlyReportsBase(models.Model):
    store_nightly_report = models.ForeignKey(StoreNightlyReport)
    no_new_item = models.BooleanField(verbose_name="Check if No New Items", default=False)
    customer = models.CharField(verbose_name='Customer Name', max_length=20, null=True)