我正在尝试自定义默认的django管理员,并在页脚中添加总字段。我不知道这是否是正确的方法。在删除模板中的{{block.super}}时,错误已删除,但我没有获得管理员的所有列表视图,仅得到了该模板的字段。
但是我也希望这些字段: image
@admin.register(Section4, site=admin_site)
class Section4Admin(ReadOnlyParamsMixin, admin.ModelAdmin):
change_list_template = 'admin/section4/section4/total_review_count.html'
def get_total(self, user):
total_n = CarePlanNeed.objects.all().count()
td = datetime.timedelta(29)
last_review = timezone.now() - td
total_review_upto_date = len(Counter([p['latest'] for p in
self.get_careplan(user).values('resident', 'aspect_of_life').annotate(
latest=Max('history_date')).filter(latest__gt=last_review).order_by() if p['latest']+td>timezone.now()]))
total_review_tobe_updated = len(Counter([p['latest'] for p in
self.get_careplan(user).values('resident', 'aspect_of_life').annotate(
latest=Max('history_date')).filter(latest__gt=last_review).order_by() if p['latest']+td<timezone.now()]))
total_review_added = self.get_careplan(user).values('resident', 'aspect_of_life')
total_review_tobe_added = total_n * 17 - len(total_review_added)
return total_review_upto_date, total_review_tobe_updated, total_review_tobe_added
def changelist_view(self, request, extra_context=None):
total_review_upto_date, total_review_tobe_updated, total_review_tobe_added = self.get_total(request.user)
my_context = {
'total_review_upto_date': total_review_upto_date,
'total_review_tobe_updated': total_review_tobe_updated,
'total_review_tobe_added': total_review_tobe_added
}
return super(Section4Admin, self).changelist_view(request,
extra_context=my_context)
total_review_count.html:
{% extends "admin/section4/section4/change_list.html" %}
{% load i18n admin_urls %}
{% block result_list %}
{{ block.super }}
<footer style="color:blue;"> <p >The total review upto date: {{ total_review_upto_date}}<br>
</p>
<p>The total review to be updated: {{ total_review_tobe_updated}}<br></p>
<p>Total review to be added : {{ total_review_tobe_added }}</p>
</footer>
<p> {{ user }}</p>
{% endblock %}
错误:AttributeError at /section4/section4/
'Section4' object has no attribute 'user'
home/bishwa/PycharmProjects/johnson/section4/admin.py in changelist_view, line 430
我实际上想在django管理员的list_view的页脚中添加total_review_tobe_updated, total_review_tobe_updated, total_review_tobe_added
。