Invoice with Items and Payment Terms Example
In this example you can see how to apply it to a view that has more than one inline formset.
# Forms
class PaymentTermForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = ModalEditFormHelper()
self.helper.layout = ModalEditLayout(
"due_date",
"amount",
)
class Meta:
model = PaymentTerm
fields = "__all__"
class InvoiceForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Row(Column("invoice_number"), Column("date")),
"client",
Fieldset(
"Items",
ModalEditFormsetLayout(
"InvoiceItemInline",
list_display=["description", "quantity", "unit_price"],
),
),
Fieldset(
"Payment Terms",
ModalEditFormsetLayout(
"PaymentTermInline",
list_display=["due_date", "amount"],
),
),
Submit("submit", "Save", css_class="btn btn-primary"),
)
class Meta:
model = Invoice
fields = "__all__"
# Inline Formset
class PaymentTermInline(InlineFormSetFactory):
model = PaymentTerm
form_class = PaymentTermForm
fields = ["due_date", "amount"]
factory_kwargs = {"extra": 0}
# View
class CreateInvoiceView(CreateWithInlinesView):
model = Invoice
inlines = [InvoiceItemInline, PaymentTermInline]
form_class = InvoiceForm