【问题标题】:Django - How to get object on another app model without direct relationship in the parent model?Django - 如何在父模型中没有直接关系的情况下在另一个应用程序模型上获取对象?
【发布时间】:2023-01-24 20:07:07
【问题描述】:

我有一个 Profile modelAward model 作为 Profile 模型的子项 [它采用其他奖励细节,如成就细节],以及一个 List_of_awards model 作为 Award 模型的子项 [它采用奖项列表和可能的计数简介有特定奖项]。

Award modelProfile model内联使用ForeignkeyList_of_awards model作为Foreignkey fieldAward model的奖项选择。

我想要做的是,在Profile model 中显示List_of_awards。 这个想法起源于我能够在 Award model', so, I was trying to link List_of_awardsin theProfile 的 list_display 中显示 List_of_awards,这没有直接关系。

class Profile(models.Model):
     first_name                          = models.CharField(verbose_name=_('First Name'), max_length=255, null=True, blank=True, )
     middle_name                         = models.CharField(verbose_name=_('Middle Name'), max_length=255, null=True, blank=True)
     last_name                           = models.CharField(verbose_name=_('Last Name'), max_length=255, null=True, blank=True)
     ....

class Award(models.Model):
    list_of_award       = models.ForeignKey('system_settings.Ribbon', related_name='awards_ad_ribbon', on_delete=models.DO_NOTHING, verbose_name=_('Type of Award'), blank=True, null=True)
    achievement         = models.TextField(verbose_name=_('Achievement'))
    profile             = models.ForeignKey('reservist_profile.Profile', related_name='awards_ad_profile', verbose_name=_('Profile'), on_delete=models.DO_NOTHING, blank=True, null=True)

     def image_tag(self):
        from django.utils.html import format_html
        return format_html('<img src="/static/media/%s" title="" width="75" /> %s' % (self.list_of_award.image,self.list_of_award.award))
     image_tag.short_description = 'Award'

class Ribbon(models.Model):
award      = models.CharField(verbose_name=_('Award'), max_length=255, null=True, blank=True)
award_desc = models.TextField(verbose_name=_('Description'), max_length=255, null=True, blank=True)
image       = models.ImageField(verbose_name = _('Image'), upload_to = award_image_location, blank = True, null = True)

class Meta:
    verbose_name = _('List of awards')
    verbose_name_plural = _('List of awards')

def __str__(self):
    return '%s' % (self.award)


def image_tag(self):
    return format_html('<img src="/static/media/%s" width="75" />' % (self.image))

image_tag.short_description = 'Award Image'

所以,这就是我目前所拥有的,其他功能是通过研究得出的,但是在这种特殊情况下,我不知道搜索关键字是什么,所以如果有人已经问过,我深表歉意。谢谢。

【问题讨论】:

    标签: python django django-models django-admin


    【解决方案1】:

    你可以这样尝试:

    class ProfileAdmin(admin.ModelAdmin):
        list_display = ('first_name',... 'list_of_awards')
        
        @admin.display(description='Awards')
        def list_of_awards(self, obj):
            return ",".join([k.list_of_award.award for k in obj.awards_ad_profile.all()]
    

    更多信息可以在documentation找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2016-03-15
      • 2019-05-17
      • 2021-02-23
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      相关资源
      最近更新 更多