gpt4 book ai didi

django - django admin admin.TabularInline有多个ForeignKey

转载 作者:行者123 更新时间:2023-12-03 15:18:12 41 4
gpt4 key购买 nike

我正在尝试在admin部分中实现带有子表单的表单。

form = Fighter()
subform = FighterFightHistory() //All of his fights

我的问题如下:
<class 'fighters.admin.Fights'>: (admin.E202) 'fighters.FighterFightHistory' has more than one ForeignKey to 'fighters.Fighter'.

因此,如何使表单显示每个外键( fighteropponent)的下拉列表。

2个外键是(请参见下文):
  • 战斗机的链接(fighter)
  • 指向对手的链接(opponent)

  • fighters / models.py
    class FighterFightHistory(TimeStampedModel):
    event = models.ForeignKey('events.Event', null=True)
    fight = models.ForeignKey('fights.Fight', null=True)
    fighter = models.ForeignKey(Fighter, related_name='%(app_label)s_%(class)s_fighter', null=True)
    howitended = models.ForeignKey('fights.HowItEnded', null=True)
    opponent = models.ForeignKey(Fighter, related_name='%(app_label)s_%(class)s_opponent', null=True)

    ended_in_round = models.IntegerField(blank=True, null=True)
    youtube_code = models.CharField(max_length=50, null=True, blank=True)
    win = models.NullBooleanField(blank=True, null=True)

    fighters / admin.py
    class Fights(admin.TabularInline):
    model = FighterFightHistory
    extra = 30


    class FighterAdmin(admin.ModelAdmin):

    list_display = ('name', 'history_completed', 'modified', 'active')
    search_fields = ['name']
    inlines = [Fights, ]

    最佳答案

    这解决了我的问题(使用 fk_name ):

    class Fights(admin.TabularInline):
    model = FighterFightHistory
    extra = 30
    fk_name = 'fighter'

    关于django - django admin admin.TabularInline有多个ForeignKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26004096/

    41 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com