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].
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.
What I am trying to do is, display the List_of_awards
in the Profile model
.
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_awardsin the
Profile` which has no direct relationship.
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. thanks.
CodePudding user response:
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.