我如何在 Django 中重构这个模型?
How can I refactor this model in Django?
我想从 Django 重构这个模型(代码),这样我就可以根据需要获取尽可能多的输入,而无需编写大量代码或高效地进行。或者我可以在模型中使用 for 循环吗?
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)
我得到了解决方案 - 只需使用 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)
这是 Django 中多对多关系的文档:https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_many/
我想从 Django 重构这个模型(代码),这样我就可以根据需要获取尽可能多的输入,而无需编写大量代码或高效地进行。或者我可以在模型中使用 for 循环吗?
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)
我得到了解决方案 - 只需使用 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)
这是 Django 中多对多关系的文档:https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_many/