[英]Django - How to get object on another app model without direct relationship in the parent model?
I have a Profile model
, Award model
as child of the Profile model [which takes other award details like achievement detail], and a List_of_awards model
as child of the Award model [which takes list of awards and a count of how may profiles has the specific award].我有一个
Profile model
, Award model
作为 Profile model 的孩子 [它采用其他奖励细节,如成就细节],以及一个List_of_awards model
作为 Award model 的孩子 [它采用奖励列表和配置文件的数量具体奖励]。
Award model
is inline with the Profile model
using Foreignkey
and List_of_awards model
as Foreignkey field
in the Award model
for the choices of awards. Award model
与Profile model
,使用Foreignkey
和List_of_awards model
作为Award model
中的Foreignkey field
用于奖项的选择。
What I am trying to do is, display the List_of_awards
in the Profile model
.我想要做的是,在
Profile model
List_of_awards
显示 List_of_awards。 The idea originated when I was able to display the List_of_awards
in the list_display
of the Award model', so, I was trying to link
List_of_awards in the
Profile` which has no direct relationship.这个想法起源于我能够在 Award 模型的
list_display
中显示List_of_awards
Award model', so, I was trying to link
Profile in the
链接 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'
So, this is what I have in the moment, other function come up through research, but this particular scenario, I have no idea what the search key word is, so I apologize if it is already ask by someone.所以,这就是我目前所拥有的,其他 function 通过研究得出,但是这种特殊情况,我不知道搜索关键字是什么,如果已经有人问过,我深表歉意。 thanks.
谢谢。
You can try like this:你可以这样尝试:
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()]
More information can be found in documentation .可以在文档中找到更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.