Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 作为外键的抽象类_Python_Django_Model View Controller_Django Models - Fatal编程技术网

Python 作为外键的抽象类

Python 作为外键的抽象类,python,django,model-view-controller,django-models,Python,Django,Model View Controller,Django Models,我试图在班级运动和区段之间建立关系。每个区段都必须选择一组机车(普通/公共汽车/火车等扩展了等级机车)。我得到的错误是“字段定义了与模型‘motion’的关系,它要么没有安装,要么是抽象的。”。有办法吗?从我读到现在,没有什么好办法,或者我错了 我的班级: class Section(RoadElements): locomotion = models.ForeignKey(Locomotion, on_delete=models.CASCADE) def __init__(se

我试图在班级运动和区段之间建立关系。每个区段都必须选择一组机车(普通/公共汽车/火车等扩展了等级机车)。我得到的错误是“字段定义了与模型‘motion’的关系,它要么没有安装,要么是抽象的。”。有办法吗?从我读到现在,没有什么好办法,或者我错了

我的班级:

class 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)

您不能有抽象基类的外键,因为该类没有数据库表。在您的示例中:将有一个数据库表,其中包含用于
普通
实例的行,但没有用于
移动
的表


通常的方法是使用指向其中一个子类的

移动是在相同的models.py中定义的。请尝试
momotion=models.ForeignKey(“momotion”,on_delete=models.CASCADE)
@trantu我以前尝试过这个,我得到了错误“字段定义了与模型“momotion”的关系,它要么没有安装,要么是抽象的。”您是否将移动声明为抽象类,如
类移动(…):class Meta:abstract=True
?@trantu是。为了更好的理解,我在问题主体中添加了声明。我不知道自己是否理解得很好,但我在堆栈溢出中发现,使用泛型外键不再有效。我弄错了吗?