I have to models Customers and Purchase_order. I am taking customer as a foreign key in purchase invoice and fetching data..
My data is like this:
{'id': 5, 'purchase_number': 'TES-PO-1', 'purchase_date': datetime.date(2022, 9, 1), 'customer_id_id': 1, 'special_instructions': '', 'total': '70', 'discount': 2.578125, 'round_off': '77.5', 'grand_total': '78', 'user_id': 1, 'remarks': '', 'company_id': 1}
{'id': 6, 'purchase_number': 'TES-PO-2', 'purchase_date': datetime.date(2022, 9, 6), 'customer_id_id': 2, 'special_instructions': '', 'total': '24', 'discount': 0.75, 'round_off': '28.5', 'grand_total': '29', 'user_id': 1, 'remarks': '', 'company_id': 1}
Action
here I am getting customer id as 1,2,3 its pk value but I wish to call its customer_company there.. how can I access them by its id on frontend?
want to show name instead of ID
views.py
def get_sales_invoices(request):
company_label= Company_Setup.objects.get(id=request.session['company_id'])
invoices=Sales_Invoice.objects.filter(company=company_label).all().values()
customer_data=[]
for c in invoices:
cust_id=c['customer_id_id']
customers_details= Customer.objects.filter(id=cust_id).all().values()
customer_data.append(customers_details)
return render(request,"sales/list_sales_invoice.html",{'invoices':invoices, 'customer_data':customer_data})
customer/models.py
class Customer(models.Model):
customer_id = models.CharField(unique=True,max_length=50)
customer_company = models.CharField(max_length=50,default=True)
title = models.CharField(max_length=100, choices=TITLE_CHOICES, default="NA")
first_name = models.CharField(max_length=50, default=True)
middle_name = models.CharField(max_length=50, default=True)
surname = models.CharField(max_length=50, default=True)
company= models.ForeignKey(Company_Setup,on_delete=models.CASCADE,default=True)
city = models.CharField(max_length=50)
state = models.CharField(max_length=50)
pin = models.CharField(max_length=50)
country = models.CharField(max_length=50)
country_code=models.CharField(max_length=50)
customer_address = models.CharField(max_length=50, default=True)
contact_person_name = models.CharField(max_length=50)
phone = models.CharField(max_length=50)
email = models.CharField(max_length=50)
website= models.CharField(max_length=50, default="NA")
GST = models.CharField(max_length=50)
bill_to = models.CharField(max_length=50)
ship_to = models.CharField(max_length=50)
payment_method = models.CharField(max_length=100, choices=PAYMENT_CHOICES, default="Normal")
terms = models.CharField(max_length=100, choices=TERMS_CHOICES, default="NA")
deffered= models.CharField(max_length=100, choices=DEFERRED_CHOICES, default="NA")
def __str__(self):
return self.customer_company
purchase/model
class Purchase_Order(models.Model):
purchase_number=models.CharField(unique=True,max_length=50,default=True)
purchase_date=models.DateField()
supplier=models.ForeignKey(Supplier,on_delete=models.CASCADE) #Foreign key
special_instructions=models.CharField(max_length=50)
total=models.CharField(max_length=50)
discount=models.FloatField(max_length=50,default=0)
round_off=models.CharField(max_length=50)
grand_total=models.CharField(max_length=50)
user =models.ForeignKey(User,on_delete=models.CASCADE)
remarks=models.CharField(max_length=50)
company=models.ForeignKey(Company_Setup,on_delete=models.CASCADE,default=True)

