Using ForeignKey in Django

Need help guys I am stuck. I've created 4 models, MasterLoan - listing of all loans, LoanExpenses - listing of all expenses incurred by loans, BillMaster - listing of all billing, BillDetails - expenses incurred by loans billed to client.

models.py

class LoanMaster(models.Model):

loan_number = models.CharField(max_length=50, null=True)
loan_name = models.CharField(max_length=150, null=True)

class LoanExpenses(models.Model):

loan_number = models.ForeignKey(LoanMaster, null=True, on_delete=models.SET_NULL)
date = models.DateField(blank=True, null=True)
expense_type = models.CharField(max_length=10, blank=True, null=True, choices=EXPENSES_TYPE)
amount = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)

class BillingMaster(models.Model):

bill_number = models.CharField(max_length=50, null=True)
bill_date = models.DateField(blank=True, null=True)
bill_amount = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
bill_status = models.CharField(max_length=50, blank=True, null=True, default='Unpaid', choices=STATUS)

class BillingDetail(models.Model):

bill_number = models.ForeignKey(BillingMaster, null=True, on_delete=models.SET_NULL)
loan_number = models.ForeignKey(LoanMaster, null=True, on_delete=models.SET_NULL, verbose_name="Loan Number")
bill_item = models.CharField(max_length=150, blank=True, null=True)
bill_amount = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)

How can I automatically pull all the expenses incurred by the loan to a dropdown select for my HTML? Should my BillingDetail.bill_item be a FK to LoanExpenses?

Back to Top