0

While using django.forms.inlineformset_factory is there a way to keep track of line numbers and pass that information to the model?

For example, consider the following in models.py

class BillLine(models.Model):
    parent = models.ForeignKey(BillHeader, on_delete=models.CASCADE)
    description = models.TextField(default = "stuff")
    quantity = models.IntegerField()
    unit_price = models.DecimalField(decimal_places = 2)
    line_number = models.IntegerField(default=0)

and the following in forms.py

class BillHeaderForm(ModelForm):
    class Meta:
        model = BillHeader
        fields = '__all__'


class BillLineForm(ModelForm):
    class Meta:
        model = BillLine
        fields = ('description', 'quantity', 'unit_price') # line number not included


BillLineFormSet = inlineformset_factory(BillHeader, BillLine, fields = ('description', 'quantity', 'unit_price'), form = BillLineForm)

When BillLineFormSet is rendered onto the view, how would I go about keeping track of line numbers?

enter image description here

Jon
  • 2,373
  • 1
  • 26
  • 34
  • I think you can include line_number as a field in the form, but not render it. – Ojas Kale Aug 27 '19 at 21:02
  • @OjasKale I thought of that, but I’m not sure how to populate the form when that field is not rendered – Jon Aug 27 '19 at 21:06
  • Possible duplicate of [How can a form within a formset know it's index within the formset?](https://stackoverflow.com/questions/25156980/how-can-a-form-within-a-formset-know-its-index-within-the-formset) – bromosapien Aug 27 '19 at 21:54

0 Answers0