python – ManyToManyField与抽象模型
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – ManyToManyField与抽象模型,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2215字,纯文字阅读大概需要4分钟。
内容图文
![python – ManyToManyField与抽象模型](/upload/InfoBanner/zyjiaocheng/769/8ff9df2cdfaa46c7abf9db7e6b854a97.jpg)
这里有一个有趣的…我缩短了模型,使其更容易理解..
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不认为通用外键是通过模型中所需的外键,并且目前没有办法在抽象基类中使用through定义ManyToMany关系.
但是,django(ticket #11760)中有一个功能请求,它允许您在通过字段中使用%(类),例如通过在Journey模型中使用through =’Pariticipant _%(class)s’,然后您必须为Journey的每个孩子手动创建viat表.但正如我所说,它只是一个开放的功能请求.
内容总结
以上是互联网集市为您收集整理的python – ManyToManyField与抽象模型全部内容,希望文章能够帮你解决python – ManyToManyField与抽象模型所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。