In a view, I have:
InvoiceFormSet = inlineformset_factory(Business,Invoice,extra=1, form = InvoiceForm)
invoice_formset = InvoiceFormSet(instance=business, data= request.POST)
def post(self, request, *args, **kwargs):
pk = self.kwargs['pk']
business = get_object_or_404(Business, pk=pk)
business_form = BusinessDetailForm(instance=business, data=request.POST)
InvoiceFormSet = inlineformset_factory(Business,Invoice,extra=1, form = InvoiceForm)
invoice_formset = InvoiceFormSet(instance=business, data= request.POST)
if 'update' in request.POST:
#messages.warning(request, request.POST)
if business_form.has_changed():
if business_form.is_valid():
business_form.save()
messages.success(request, 'Les données Affaires ont été mises à jour.')
else:
messages.warning(request, 'les données Affaires sont incorrectes.'+ str(business_form.errors))
if invoice_formset.has_changed():
if invoice_formset.is_valid():
for form in invoice_formset.forms:
if form.has_changed():
form.save()
InvoiceForm looks like this:
class InvoiceForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['number'].initial = self.instance.number
number = forms.CharField(disabled=True, required=False)
class Meta:
model = Invoice
fields= ['label','issuance_date','due_date','type','amount', 'is_issued', 'is_paid','payment_date']
widgets = {'issuance_date': DatePickerInput, 'due_date': DatePickerInput,'payment_date':DatePickerInput}
In the model Invoice is as follows, with several fields with default value
class Invoice(models.Model):
number = models.CharField('Numéro de facture', max_length=20, null=True, blank=True)
label = models.CharField('libellé de facture', max_length=200, null=True,blank = True)
issuance_date = models.DateField('Date de facture', default=datetime.date.today)
due_date = models.DateField('Date d\'échéance', default=default_due_date)
PREVISION = 'PR'
FACTURE = 'FA'
AVOIR = 'AV'
INVOICE_TYPE_CHOICES = [
(PREVISION, 'Echéance'),
(FACTURE, 'Facture'),
(AVOIR, 'Avoir')
]
type = models.CharField('Type de facture',max_length=2,
choices=INVOICE_TYPE_CHOICES, default=PREVISION)
amount = models.DecimalField('Montant',max_digits=9, decimal_places=2, default=0)
is_issued = models.BooleanField('Emise', default=False)
is_paid = models.BooleanField('Réglée', default=False)
payment_date = models.DateField('Date de règlement', null=True,blank=True)
business = models.ForeignKey(Business, on_delete=models.CASCADE, verbose_name='Commande')
market = models.ForeignKey(Market, on_delete=models.RESTRICT, null=True,blank=True)
purchase_order = models.ForeignKey(Purchase_order, on_delete=models.RESTRICT, null=True,blank=True)
customer_ref = models.CharField('Référence client', max_length=250,
null=True, blank=True)
customer_contact = models.ForeignKey(Contact, on_delete=models.DO_NOTHING, null=True, blank=True,
verbose_name='Suivi par')
addressee = models.CharField('Destinataire', max_length=60, null=True, blank=True)
address = models.ForeignKey(Address, on_delete=models.RESTRICT,
verbose_name='Adresse de facturation', null=True, blank = True)
When the forms are displayed, there is an extra line with some fields beign filled with default values.
When the form is submited, a new invoice a created even if I do not enter anything in the last line (this form has not changed).
How to prevent this creation?