Model" Playbook"有相关的子对象"活动" 需要list_display"活动"在django admin中为每个" playbook"宾语。 目前,list_display显示所有playbook对象(父对象)的所有活动对象。
class PlayBook(TimeStampModel):
name = models.CharField(max_length=200, unique=True)
description = models.TextField(blank=True, help_text="Business purpose of the application")
owner = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
ordering = ('name',)
@property
def short_description(self):
return truncatechars(self.description, 35)
def __str__(self):
return "{}".format(self.name)
class Activity(TimeStampModel):
minor = 'MINOR'
normal = 'NORMAL'
important = 'IMPORTANT'
critical = 'CRITICAL'
SEVERITY = (
(minor, 'Minor'),
(normal, 'Normal'),
(important, 'Important'),
(critical, 'Critical'),
)
low = 'LOW'
high = 'HIGH'
PRIORITY = (
(low, 'Low'),
(normal, 'Normal'),
(high, 'High'),
)
new = 'New'
in_progress = 'In_Progress'
needs_info = 'Needs_Info'
postponed = 'Postponed'
closed = 'Closed'
STATUS= (
(new, 'New'),
(in_progress, 'In_Progress'),
(needs_info, 'Needs_Info'),
(postponed, 'Postponed'),
(closed, 'Closed'),
)
playbook = models.ForeignKey(PlayBook)
subject = models.CharField(max_length=200, unique=True)
description = models.TextField(blank=True, help_text="Business purpose of the application")
manager = models.ForeignKey(User, on_delete=models.CASCADE)
severity = models.CharField(max_length = 100, choices=SEVERITY, default=normal)
priority = models.CharField(max_length = 100, choices=PRIORITY, default=normal)
status = models.CharField(max_length = 100, choices=STATUS, default=new)
def __str__(self):
return "{}".format(self.subject)
class Meta:
ordering = ('severity',)
@property
def short_description(self):
return truncatechars(self.description, 35)
class ReviewActivity(Activity):
class Meta:
proxy=True
def __str__(self):
return self.subject.upper()
class ActivityAdmin(admin.TabularInline):
list_display =['severity','priority', 'subject', 'status_colored','created','updated', 'short_description']
def status_colored(self, obj):
colors = {
'Closed': '#DCE775',
'Needs_Info': '#F8BBD0' ,
'In_Progress': '#FFCCBC',
'New':'#81D4FA',
'Postponed': '#CFD8DC'
}
return format_html(
'<b style="background:{};">{}</b>',
colors[obj.status],
obj.status,
)
class ActivityInline(admin.TabularInline):
model = Activity
extra = 1
class PlayBookAdmin(admin.ModelAdmin):
inlines = [
ActivityInline,
]
list_display =['name','short_description','created', 'updated','owner',]
class ReviewActivityAdmin(admin.ModelAdmin):
list_display =['severity','priority', 'subject', 'status_colored','created','updated', 'short_description']
def status_colored(self, obj):
colors = {
'Closed': '#DCE775',
'Needs_Info': '#F8BBD0' ,
'In_Progress': '#FFCCBC',
'New':'#81D4FA',
'Postponed': '#CFD8DC'
}
return format_html(
'<b style="background:{};">{}</b>',
colors[obj.status],
obj.status,
)
#admin.site.register(Activity, ActivityAdmin)
admin.site.register(PlayBook, PlayBookAdmin)
admin.site.register(ReviewActivity, ReviewActivityAdmin)
Playbook链接显示以下内容: playbook1显示表格显示
查看活动链接显示所有剧本的所有活动对象。需要显示相关剧本的活动对象。并非所有剧本* 。 见(图4):
答案 0 :(得分:0)
今天早上我刚遇到类似的情况,这就是我解决问题的方法。我将用你的例子来解释。
我在PlayBook模型中定义了一个称为activity()的新方法,如下所示:
class PlayBook(TimeStampModel):
name = models.CharField(max_length=200, unique=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
def activities(self):
qs = self.activity_set.all() # this gets all the child related objects
for x in qs:
result = x.subject # here i am just getting one of the fields from the Activities model
return result
因此,现在在您的admin.py中,您可以按以下方式设置list_diplay
class PlayBookAdmin(admin.ModelAdmin):
list_display =['activities',] # This will call the activities() method that your created in the models.py and display the result.