带有 ModelBase 的 Django Formsets 不渲染复选框默认值而是一个下拉列表

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

如果 no_new_item 存在,我需要创建一个必需的复选框选项。我正在使用 model.NullBoolean 字段。根据 Django 文档,布尔字段应呈现复选框小部件,但 NullBoolean 呈现 Select。之所以改用NullBoolean,是因为迁移时出现null错误。所以现在我得到一个带有 'Yes' 和 'No.' 的下拉列表 我将如何使用 NullBoolean 在 Base model.Models 中创建复选框,还是有更好的方法?

(这是我想念的关于 Django 文档的编辑。谢谢@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>

固定

经过一番思考后,我回到了 model.Base 并将其更改为 BooleanField 类型并添加了默认值。这清除了迁移时的空错误并呈现了一个复选框选项。

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)