前提・実現したいこと
同じappの中で1つのmodelを作成して似た内容のmodelを複数作成する方法はありませんか?
発生している問題・エラーメッセージ
具体的にはこちらに記載した、 classと2行目、class Meta以外は同じ内容を繰り返して、別のmodelを作成しています。 内容の変更を行う際に全部で12個のmodelを一つずつ直しているのですが、 時間がかかるので何とかしたく。 わかる方ご教授ください。 class InsLedger1(models.Model): measuring_ins_group = models.IntegerField(choices=MEASURING_INS_GROUP_CHOICES,default=1) ---------------------------------------------- class Meta: verbose_name = "マイクロメータ"
該当のソースコード
python
1models.py 2 3class InsLedger1(models.Model): 4 measuring_ins_group = models.IntegerField(choices=MEASURING_INS_GROUP_CHOICES,default=1) 5 measuring_tool = models.ForeignKey(Device, on_delete=models.CASCADE) 6 ins_num = models.CharField(max_length=10) 7 calibration_type = models.IntegerField(choices=CALIBRATION_CHOICES)#校正期間 8 calibration_date = models.DateTimeField(default=now)#datetime.datetime.now()) 9 next_calibration_date = models.DateTimeField() 10 serial_num = models.CharField(max_length=20, null=True, blank=True, default='') 11 product_num = models.ForeignKey(Product_num, on_delete=models.CASCADE)#対象品番 12 team = models.ForeignKey(TeamModel, on_delete=models.CASCADE) 13 line = models.ForeignKey(LineModel, on_delete=models.CASCADE) 14 useapp = models.ForeignKey(Useapp, on_delete=models.CASCADE)#使用用途 15 spec1 = models.ForeignKey(Spec1, on_delete=models.CASCADE)#最小目盛 16 spec2 = models.ForeignKey(Spec2, on_delete=models.CASCADE)#測定範囲 17 spec3 = models.IntegerField(choices=SPEC3_CHOICES)#表示 18 item = models.ForeignKey(Item, on_delete=models.CASCADE)#日常点検項目 19 20 def save(self, *args, **kwargs): 21 auto_now = kwargs.pop('next_calibration_date_auto_now', True) 22 if auto_now: 23 if self.calibration_type == CALIBRATION_CHOICES[0]: 24 calibration_span = 1 25 elif self.calibration_type == CALIBRATION_CHOICES[1]: 26 calibration_span = 2 27 elif self.calibration_type == CALIBRATION_CHOICES[2]: 28 calibration_span = 5 29 elif self.calibration_type == CALIBRATION_CHOICES[3]: 30 calibration_span = 10 31 self.next_calibration_date = self.calibration_date + relativedelta(years=calibration_span) 32 super().save(*args, **kwargs) 33 34 def __str__(self): 35 return str(self.measuring_tool) 36 37 class Meta: 38 verbose_name = "マイクロメータ" 39 40 41class InsLedger2(models.Model): 42 measuring_ins_group = models.IntegerField(choices=MEASURING_INS_GROUP_CHOICES,default=2) 43 measuring_tool = models.ForeignKey(Device, on_delete=models.CASCADE) 44 ins_num = models.CharField(max_length=10) 45 calibration_type = models.IntegerField(choices=CALIBRATION_CHOICES)#校正期間 46 calibration_date = models.DateTimeField(default=now)#datetime.datetime.now()) 47 next_calibration_date = models.DateTimeField() 48 serial_num = models.CharField(max_length=20, null=True, blank=True, default='') 49 product_num = models.ForeignKey(Product_num, on_delete=models.CASCADE)#対象品番 50 team = models.ForeignKey(TeamModel, on_delete=models.CASCADE) 51 line = models.ForeignKey(LineModel, on_delete=models.CASCADE) 52 useapp = models.ForeignKey(Useapp, on_delete=models.CASCADE)#使用用途 53 spec1 = models.ForeignKey(Spec1, on_delete=models.CASCADE)#最小目盛 54 spec2 = models.ForeignKey(Spec2, on_delete=models.CASCADE)#測定範囲 55 spec3 = models.IntegerField(choices=SPEC3_CHOICES)#表示 56 item = models.ForeignKey(Item, on_delete=models.CASCADE)#日常点検項目 57 58 def save(self, *args, **kwargs): 59 auto_now = kwargs.pop('next_calibration_date_auto_now', True) 60 if auto_now: 61 if self.calibration_type == CALIBRATION_CHOICES[0]: 62 calibration_span = 1 63 elif self.calibration_type == CALIBRATION_CHOICES[1]: 64 calibration_span = 2 65 elif self.calibration_type == CALIBRATION_CHOICES[2]: 66 calibration_span = 5 67 elif self.calibration_type == CALIBRATION_CHOICES[3]: 68 calibration_span = 10 69 self.next_calibration_date = self.calibration_date + relativedelta(years=calibration_span) 70 super().save(*args, **kwargs) 71 72 def __str__(self): 73 return str(self.measuring_tool) 74 75 class Meta: 76 verbose_name = "ノギス"
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/23 06:58