在我的Django应用程序中,我有Profile
和Report
个模型。
https://ibb.co/b9YwsH
在Django管理员中,我可以查看报告和个人资料。
报告包含变量Toxicity
和Sportsmanship
。
我正在尝试获取变量TOXICITY
和SPORTSMANSHIP
并将其总数显示为管理员PROFILE
视图中的字段(例如总数:25)
https://ibb.co/bVBrsH
错误:
<class 'apps.api.admin.ProfileAdmin'>: (admin.E108) The value of
'list_display[2]' refers to 'user_report', which is not a callable, an
attribute of 'ProfileAdmin', or an attribute or method on 'api.Profile'.
代码:
from django.contrib import admin
from ..api.models import Profile,Report,
Game_Api_Connection, Feedback
class ReportAdmin(admin.ModelAdmin):
def user_report(self):
total = TOXICITY + SPORTSMANSHIP
return self.user.total
admin.site.register(Report,ReportAdmin)
class ProfileAdmin(admin.ModelAdmin):
list_display = ('birth_date', 'sessions_played', 'user_report')
admin.site.register(Profile, ProfileAdmin)
答案 0 :(得分:0)
如果您希望user report
管理员Profile
,则应在Profile
admin中实施您的方法。
class ProfileAdmin(admin.ModelAdmin):
list_display = ('birth_date', 'sessions_played', 'user_report')
def user_report(self, obj):
# your logic should be here
# You should get all `obj`(current profile) reports here
return total
这就是管理列表显示在同一个类和模型中查找属性或可调用的原因。
答案 1 :(得分:0)
试试这段代码:
from django.contrib import admin
from .api.models import Profile,Report, Game_Api_Connection, Feedback
class ReportAdmin(admin.ModelAdmin):
pass
admin.site.register(Report,ReportAdmin)
class ProfileAdmin(admin.ModelAdmin):
list_display = ('birth_date', 'sessions_played', 'user_report')
def user_report(self):
total = self.report.TOXICITY + self.report.SPORTSMANSHIP
return total
admin.site.register(Profile, ProfileAdmin)