有什么办法可以在CourseAdmin的bin
中显示参加课程的总数?
admin.py
list_display
我尝试了以下操作:
class Attendee_Inline(admin.TabularInline):
model = Attendee
extra = 5
class CourseAdmin(admin.ModelAdmin):
inlines = [Attendee_Inline]
list_display = ('date','course_name','course','venue','maximum_attendees','get_count')
答案 0 :(得分:0)
在课程模型中编写一个函数,该函数将返回参加者的总数。
models.py
class Course(models.Model):
....
def attendee_count(self):
q = Attendee.objects.filter(course_id=self.id).count()
return q
attendee_count.allow_tags = True
在CourseAdmin的列表显示中添加“出席人数”。
class CourseAdmin(admin.ModelAdmin):
inlines = [Attendee_Inline]
list_display = ('attendee_count',............)