這裏有一個有趣的..我已經縮短了模型,使其更容易理解..ManyToManyField通過對抽象模型
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'
的ParticipantJourney模型參與者鏈接到一個旅程,現在旅程是抽象的,因爲它可以通過任意數量的不同運輸方式製成,每種運輸方式都有各自的領域。我覺得這個設置是正確的,但我發現了以下錯誤消息:
Error: One or more models did not validate: kandersteg.planejourney: 'participants' is a manually-defined m2m relation through model ParticipantJourney, which does not have foreign keys to Participant and PlaneJourney
我需要保持鏈接表的手工定義,所以我還可以鏈接進行付款,說的旅程,所以我真的不知道下一步該怎麼做,如果任何人都可以擺脫一些光,我會非常感激!
乾杯,亞歷克斯
是的工作,但擊敗了它作爲抽象的對象:( –
增加了參與者模型,它減少了很多(以及所有的模型都很好),但你得到的主意 –
我個人認爲這可能是一個錯誤django是因爲我查看了生成這個代碼的代碼,當它尋找兩個ForeignKeys來構成ManyToMany時,它似乎沒有考慮到GenericForeignKey字段。 –