title: How do I refactor my Purchase and Sales models by the method of DRY
body: <p>I am making a Medical Billing Application with the Django framework but my models need refactoring so that they can work on the principle of DRY(Don't Repeat Yourself). Please help me out with this! Here are the files:</p>
  <p><strong>models.py(Medicines Model)</strong></p>
  <pre><code>#------------------------Medicine models----------------------------------
  gst = (
      (0, '0%'),
      (5, '5%'),
      (12, '12%'),
      (18, '18%'),
  )
  # Create your models here.
  class Medicines(models.Model):
      Med_HSN = models.IntegerField(default=0)
      Med_name = models.CharField(max_length=20)
      Med_Quantity = models.PositiveIntegerField(default=0)
      Med_Manufacturer = models.CharField(blank=True,max_length=50)
      Med_Expiry = models.CharField(default=1,max_length=10)
      Med_Batch_no = models.CharField(blank=True,max_length=15)
      Med_MRP = models.FloatField(blank=False)
      Med_Rate = models.FloatField(blank=False)
      Med_GST = models.IntegerField(default=0,choices=gst)
      date_created = models.DateField(default=timezone.now)
  
      def caluclate_value(self):
          value = self.Med_MRP * (self.Med_GST/100)
          total_value = self.Med_MRP + value 
          return total_value
  
      def get_cost(self):
          return self.caluclate_value() * self.Med_Quantity
  
      def __str__(self):
          return self.Med_name + &quot;  |  &quot; + str(self.caluclate_value()) + &quot; || Quantity in Stock : &quot; + str(self.Med_Quantity)
  
  #----------------------------------End Medicines Models--------------------------------------
  </code></pre>
  <p><strong>models.py(Sales Model)</strong></p>
  <pre><code>#---------------------------------Sales Models--------------------------------
  from django.core.checks import messages
  from django.db import models
  from Customer.models import Customer
  from django.utils import timezone
  import math
  
  payment_status = (
      ('Due', 'Due'),
      ('Paid|Cash', 'Paid|Cash'),
      ('Paid|Cheque', 'Paid|Cheque'),
      ('Paid|Online', 'Paid|Online'),
      ('Cancelled','Cancelled')
  )
  dis = (
      (0, '0%'),
      (5, '5%'),
      (10, '10%'),
      (15, '15%'),
      (20, '20%'),
  )
  # Create your models here.
  class Sales(models.Model):
      # sales_id = models.AutoField(primary_key=True)
      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)
      discount_1 = models.FloatField(choices=dis,default=0)
      rate_1 = models.FloatField(blank=True,default=0)
      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,null=True,default=0)
      Free_quantity_2 = models.IntegerField(blank=True,default=0)
      discount_2 = models.FloatField(choices=dis,default=0)
      rate_2 = models.FloatField(blank=True,default=0)
      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,null=True,default=0)
      Free_quantity_3 = models.IntegerField(blank=True,default=0)
      discount_3 = models.FloatField(choices=dis,default=0)
      rate_3 = models.FloatField(blank=True,default=0)
      sales_medicine_4 = models.ForeignKey(Medicines,blank=True,null=True,related_name='sales_medicine_3',on_delete=models.CASCADE)
      Med_quantity_4 = models.IntegerField(blank=True,null=True,default=0)
      Free_quantity_4 = models.IntegerField(blank=True,default=0)
      discount_4 = models.FloatField(choices=dis,default=0)
      rate_4 = models.FloatField(blank=True,default=0)
      sales_medicine_5 = models.ForeignKey(Medicines,blank=True,null=True,related_name='sales_medicine_4',on_delete=models.CASCADE)
      Med_quantity_5 = models.IntegerField(blank=True,null=True,default=0)
      Free_quantity_5 = models.IntegerField(blank=True,default=0)
      discount_5 = models.FloatField(choices=dis,default=0)
      rate_5 = models.FloatField(blank=True,default=0)
      sales_medicine_6 = models.ForeignKey(Medicines,blank=True,null=True,related_name='sales_medicine_5',on_delete=models.CASCADE)
      Med_quantity_6 = models.IntegerField(blank=True,null=True,default=0)
      Free_quantity_6 = models.IntegerField(blank=True,default=0)
      discount_6 = models.FloatField(choices=dis,default=0)
      rate_6 = models.FloatField(blank=True,default=0)
      sales_medicine_7 = models.ForeignKey(Medicines,blank=True,null=True,related_name='sales_medicine_6',on_delete=models.CASCADE)
      Med_quantity_7 = models.IntegerField(blank=True,null=True,default=0)
      Free_quantity_7 = models.IntegerField(blank=True,default=0)
      discount_7 = models.FloatField(choices=dis,default=0)
      rate_7 = models.FloatField(blank=True,default=0)
      sales_medicine_8 = models.ForeignKey(Medicines,blank=True,null=True,related_name='sales_medicine_7',on_delete=models.CASCADE)
      Med_quantity_8 = models.IntegerField(blank=True,null=True,default=0)
      Free_quantity_8 = models.IntegerField(blank=True,default=0)
      discount_8 = models.FloatField(choices=dis,default=0)
      rate_8 = models.FloatField(blank=True,default=0)
      sales_medicine_9 = models.ForeignKey(Medicines,blank=True,null=True,related_name='sales_medicine_8',on_delete=models.CASCADE)
      Med_quantity_9 = models.IntegerField(blank=True,null=True,default=0)
      Free_quantity_9 = models.IntegerField(blank=True,default=0)
      discount_9 = models.FloatField(choices=dis,default=0)
      rate_9 = models.FloatField(blank=True,default=0)
      sales_medicine_10 = models.ForeignKey(Medicines,blank=True,null=True,related_name='sales_medicine_9',on_delete=models.CASCADE)
      Med_quantity_10 = models.IntegerField(blank=True,null=True,default=0)
      Free_quantity_10 = models.IntegerField(blank=True,default=0)
      discount_10 = models.FloatField(choices=dis,default=0)
      rate_10 = models.FloatField(blank=True,default=0)
      Payment_status = models.CharField(max_length=15,choices=payment_status)
      Paid_amount = models.FloatField(blank=True,default=0)
  
      def __str__(self):
          return self.customer.customer_name
  </code></pre>
  <p><strong>models.py(Purchase Model)</strong></p>
  <pre><code>from django.db import models
  from django.utils import timezone
  from Wholeseller.models import Wholeseller
  import math
  
  payment_status = (
      ('Due', 'Due'),
      ('Paid|Cash', 'Paid|Cash'),
      ('Paid|Cheque', 'Paid|Cheque'),
      ('Paid|Online', 'Paid|Online'),
      ('Cancelled','Cancelled')
  )
  
  class Purchase(models.Model):
      # purchase_id = models.AutoField(primary_key=True)
      wholeseller = models.ForeignKey(Wholeseller,on_delete=models.CASCADE)
      date_created = models.DateTimeField(default=timezone.now)
      delivery_date = models.CharField(blank=True,max_length=20)
      medicine_1 = models.ForeignKey(Medicines,related_name=&quot;medicine_1&quot;,on_delete=models.CASCADE)
      med_quantity_1 = models.IntegerField(blank=False,default=1)
      Free_Quantitiy_1 = models.IntegerField(blank=True,default=0)
      discount_1 = models.FloatField(blank=True,default=0)
      medicine_2 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_2&quot;,on_delete=models.CASCADE)
      med_quantity_2 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_2 = models.IntegerField(blank=True,default=0)
      discount_2 = models.FloatField(blank=True,default=0)
      medicine_3 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_3&quot;,on_delete=models.CASCADE)
      med_quantity_3 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_3 = models.IntegerField(blank=True,default=0)
      discount_3 = models.FloatField(blank=True,default=0)
      medicine_4 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_4&quot;,on_delete=models.CASCADE)
      med_quantity_4 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_4 = models.IntegerField(blank=True,default=0)
      discount_4 = models.FloatField(blank=True,default=0)
      medicine_5 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_5&quot;,on_delete=models.CASCADE)
      med_quantity_5 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_5 = models.IntegerField(blank=True,default=0)
      discount_5 = models.FloatField(blank=True,default=0)
      medicine_6 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_6&quot;,on_delete=models.CASCADE)
      med_quantity_6 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_6 = models.IntegerField(blank=True,default=0)
      discount_6 = models.FloatField(blank=True,default=0)
      medicine_7 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_7&quot;,on_delete=models.CASCADE)
      med_quantity_7 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_7 = models.IntegerField(blank=True,default=0)
      discount_7 = models.FloatField(blank=True,default=0)
      medicine_8 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_8&quot;,on_delete=models.CASCADE)
      med_quantity_8 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_8 = models.IntegerField(blank=True,default=0)
      discount_8 = models.FloatField(blank=True,default=0)
      medicine_9 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_9&quot;,on_delete=models.CASCADE)
      med_quantity_9 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_9 = models.IntegerField(blank=True,default=0)
      discount_9 = models.FloatField(blank=True,default=0)
      medicine_10 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_10&quot;,on_delete=models.CASCADE)
      med_quantity_10 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_10 = models.IntegerField(blank=True,default=0)
      discount_10 = models.FloatField(blank=True,default=0)
      medicine_11 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_11&quot;,on_delete=models.CASCADE)
      med_quantity_11 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_11 = models.IntegerField(blank=True,default=0)
      discount_11 = models.FloatField(blank=True,default=0)
      medicine_12 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_12&quot;,on_delete=models.CASCADE)
      med_quantity_12 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_12 = models.IntegerField(blank=True,default=0)
      discount_12 = models.FloatField(blank=True,default=0)
      medicine_13 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_13&quot;,on_delete=models.CASCADE)
      med_quantity_13 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_13 = models.IntegerField(blank=True,default=0)
      discount_13 = models.FloatField(blank=True,default=0)
      medicine_14 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_14&quot;,on_delete=models.CASCADE)
      med_quantity_14 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_14 = models.IntegerField(blank=True,default=0)
      discount_14 = models.FloatField(blank=True,default=0)
      medicine_15 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_15&quot;,on_delete=models.CASCADE)
      med_quantity_15 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_15 = models.IntegerField(blank=True,default=0)
      discount_15 = models.FloatField(blank=True,default=0)
      medicine_16 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_16&quot;,on_delete=models.CASCADE)
      med_quantity_16 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_16 = models.IntegerField(blank=True,default=0)
      discount_16 = models.FloatField(blank=True,default=0)
      medicine_17 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_17&quot;,on_delete=models.CASCADE)
      med_quantity_17 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_17 = models.IntegerField(blank=True,default=0)
      discount_17 = models.FloatField(blank=True,default=0)
      medicine_18 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_18&quot;,on_delete=models.CASCADE)
      med_quantity_18 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_18 = models.IntegerField(blank=True,default=0)
      discount_18 = models.FloatField(blank=True,default=0)
      medicine_19 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_19&quot;,on_delete=models.CASCADE)
      med_quantity_19 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_19 = models.IntegerField(blank=True,default=0)
      discount_19 = models.FloatField(blank=True,default=0)
      medicine_20 = models.ForeignKey(Medicines,blank=True,null=True,related_name=&quot;medicine_20&quot;,on_delete=models.CASCADE)
      med_quantity_20 = models.IntegerField(blank=True,default=0)
      Free_Quantitiy_20 = models.IntegerField(blank=True,default=0)
      discount_20 = models.FloatField(blank=True,default=0)
      Payment_status = models.CharField(max_length=15,choices=payment_status)
      Paid_amount = models.FloatField(blank=True,default=0)
      # Due_amount = models.IntegerField(blank=True,default=0)
      Discount_amount = models.FloatField(blank=True,default=0)
      # Total_amount = models.IntegerField(blank=True)
  
      def __str__(self): 
          return self.wholeseller.wholeseller_name
  </code></pre>
  <p>By seeing the models you have now understood why I need refactoring so please help out.</p>