I want to refactor this model(code) from Django so that I can take as many inputs as required without writing a lot of code or efficiently. Or Can I use for loop in models?
models.py
class Sales(models.Model):
customer = models.ForeignKey(Customer,on_delete=models.CASCADE)
prescribed_doctor = models.CharField(blank=True,max_length=150)
date_created = models.DateTimeField(default=timezone.now)
sales_medicine_1 = models.ForeignKey(Medicines,related_name='sales_medicine',on_delete=models.CASCADE)
Med_quantity_1 = models.IntegerField(blank=False,default=1)
Free_quantity_1 = models.IntegerField(blank=True,default=0,null=True)
margin_1 = models.PositiveIntegerField(blank=True,default=0,null=True)
discount_1 = models.FloatField(choices=dis,default=0,null=True)
discount_1_amt = models.FloatField(default=0,null=True)
rate_1 = models.FloatField(blank=True,default=0,null=True)
batch_no_1 = models.CharField(null=True,blank=True,max_length=50)
mfg_1 = models.CharField(max_length=50,blank=True,null=True)
expiry_1 = models.CharField(max_length=20,null=True,blank=True)
sales_medicine_2 = models.ForeignKey(Medicines,blank=True,null=True,related_name='sales_medicine_1',on_delete=models.CASCADE)
Med_quantity_2 = models.IntegerField(blank=True,default=0,null=True)
Free_quantity_2 = models.IntegerField(blank=True,default=0,null=True)
margin_2 = models.PositiveIntegerField(blank=True,default=0,null=True)
discount_2 = models.FloatField(choices=dis,default=0,null=True)
discount_2_amt = models.FloatField(default=0,null=True)
rate_2 = models.FloatField(blank=True,default=0,null=True)
batch_no_2 = models.CharField(null=True,blank=True,max_length=50)
mfg_2 = models.CharField(max_length=50,blank=True,null=True)
expiry_2 = models.CharField(max_length=20,null=True,blank=True)
sales_medicine_3 = models.ForeignKey(Medicines,blank=True,null=True,related_name='sales_medicine_2',on_delete=models.CASCADE)
Med_quantity_3 = models.IntegerField(blank=True,default=0,null=True)
Free_quantity_3 = models.IntegerField(blank=True,default=0,null=True)
margin_3 = models.PositiveIntegerField(blank=True,default=0,null=True)
discount_3 = models.FloatField(choices=dis,default=0,null=True)
discount_3_amt = models.FloatField(default=0,null=True)
rate_3 = models.FloatField(blank=True,default=0,null=True)
batch_no_3 = models.CharField(null=True,blank=True,max_length=50)
mfg_3 = models.CharField(max_length=50,blank=True,null=True)
expiry_3 = models.CharField(max_length=20,null=True,blank=True)
Payment_status = models.CharField(max_length=15,choices=payment_status)
Paid_amount = models.FloatField(blank=True,default=0,null=True)
auto_generated = models.BooleanField(default=False)
CodePudding user response:
I got the solution - just use ManytoManyField()
.
class Sales(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
prescribed_doctor = models.CharField(blank=True, max_length=150)
date_created = models.DateTimeField(default=timezone.now)
sales_medicine = models.ManytoManyField(Medicines)
Here is the documentation to many to many relationships in Django: https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_many/