クラス移動とセクションの間の関係を作成しようとしています。すべてのセクションには、ロコモーションの王が選ばれなければなりません(クラス移動は平野/バス/電車などで拡張されます)。そして、私は "Fieldはモデル" Locomotion "との関係を定義します。これはインストールされていないか抽象です"。それを行う方法はありますか?私が今までに読んだことから、それを行う良い方法はない、あるいは私は間違っている?外部キーとしての抽象クラス
私のクラスのセクション:
だけでなく、抽象クラスを拡張しRoadElementsclass Section(RoadElements):
locomotion = models.ForeignKey(Locomotion, on_delete=models.CASCADE)
def __init__(self, *args, **kwargs):
super(Section, self).__init__(*args, **kwargs)
def __str__(self):
return self.name
。
歩行の私の定義:
class Locomotion(models.Model):
transportation_firm_name = models.CharField(max_length=200)
transportation_number = models.CharField(max_length=200)
departure_date_time = models.DateTimeField()
arrival_date_time = models.DateTimeField()
reservation = models.BooleanField(default=False)
class Meta:
abstract = True
とクラス例えば、拡張:
class Plain(Locomotion):
seat_number = models.CharField(max_length=200)
class_section = models.CharField(max_length=200)
** Locomotion **は同じアプリまたは同じmodels.pyで定義されていますか? – trantu
Locomotionは同じmodels.pyで定義されています。 – Yurrili
試してみてください 'locomotion = models.ForeignKey(" Locomotion "、on_delete = models.CASCADE)' – trantu