Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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_Many To Many_Abstract_Django Orm - Fatal编程技术网

Python 通过对抽象模型的分析,得出了许多结论

Python 通过对抽象模型的分析,得出了许多结论,python,django,many-to-many,abstract,django-orm,Python,Django,Many To Many,Abstract,Django Orm,这里有一个有趣的。。我缩短了模型以便于理解 class Participant(Person): passport_number = models.IntegerField(verbose_name=_('Passport Number'), db_column=u'PassportNumber') class Meta: db_table = u'Participant' class Journey(BaseModel): participants

这里有一个有趣的。。我缩短了模型以便于理解

class Participant(Person):
    passport_number = models.IntegerField(verbose_name=_('Passport Number'), db_column=u'PassportNumber')

    class Meta:
        db_table = u'Participant'

class Journey(BaseModel):
    participants = models.ManyToManyField(Participant, related_name='%(app_label)s_%(class)s_participants', through=u'ParticipantJourney')

    class Meta:
        abstract = True

class PlaneJourney(Journey):
    flight_number = models.CharField(max_length=16, verbose_name=_('Flight Number'), db_column=u'FlightNumber')

    class Meta:
        db_table = u'PlaneJourney'

class ParticipantJourney(BaseModel):
    participant = models.ForeignKey(Participant, verbose_name=_('Participant'), db_column=u'ParticipantId')

    journey_content_type = models.ForeignKey(ContentType, related_name='journey_content_type')
    journey_object_id = models.PositiveIntegerField()
    journey = generic.GenericForeignKey('journey_content_type', 'journey_object_id') # models.ForeignKey(Journey, verbose_name=_('Journey'), db_column=u'JourneyId')

    payment_content_type = models.ForeignKey(ContentType, related_name='payment_content_type')
    payment_object_id = models.PositiveIntegerField()
    payment = generic.GenericForeignKey('payment_content_type', 'payment_object_id') # models.ForeignKey(Payment, verbose_name=_('Payment'), db_column=u'PaymentId')

    class Meta:
        db_table = u'ParticipantJourney'
ParticipantTravel模型将参与者与旅程联系起来,现在的旅程是抽象的,因为它可以由任意数量的不同交通方式完成,每个交通方式都有各自的字段。我认为此设置是正确的,但我收到以下错误消息:

错误:一个或多个模型未验证: kandersteg.PlaneTourney:“参与者”是通过模型ParticipantTourney手动定义的m2m关系,它没有参与者和PlaneTourney的外键

我需要保留链接表的手动定义,这样我也可以将付款链接到所述旅程,这样我真的不知道下一步该怎么做,如果有人能透露一些信息,我将非常感谢

干杯,
Alex

您可以尝试将旅程定义为非抽象的,但随后会将数据拆分到多个表中


我在这里发现了一些东西:

django不考虑通用外键作为贯穿模型中所需的外键,并且目前没有办法在抽象基类中定义与泛型的关系。


但是,django()中有一个功能请求,它允许您在through字段中也使用
%(class)s
,例如,在
旅程
模型中使用
through='pariticant'(class)s'
,然后您必须为
旅程
的每个子项手动创建through表。但正如我所说的,这只是一个开放的功能要求。

是的,这是可行的,但无法实现将其作为抽象的目标:(添加了参与者模型,它大大精简了(所有模型也是如此)但是你得到的gistI个人认为这可能是django中的一个bug,因为我看了生成它的代码,它似乎没有考虑到GenericForeignKey字段,当它在寻找两个ForeignKey来组成manytomany时,它真的会那么糟糕吗?不,不会,但我想我想看看是否有人知道在放弃之前先让它工作的方法。。