您还可以使用“change_list_template”覆盖您用于管理员的模板,然后扩展 change_list 模板。喜欢
class NoiseFilter200Admin(admin.ModelAdmin):
change_list_template = 'currencies/nf200_change_list.html'
list_filter = ['sectors',]
search_fields = ['name']
#list_filter = ( MarketCapFilter )
def market_cap_value(self, obj):
return obj.market_cap_value > 1000000000
def changelist_view(self, request, extra_context=None):
response = super().changelist_view(
request,
extra_context=extra_context,
)
try:
qs = response.context_data['cl'].queryset
except (AttributeError, KeyError):
return response
metrics = {
'total_volume': Sum('volume'),
'total_mcap': Sum('market_cap'),
}
response.context_data['nf_200_list'] = list(
qs
.values('rank','ticker','name','priceUSD','market_cap','volume','change_24h')
# .annotate(**metrics)
.order_by('-market_cap')[:200]
)
response.context_data['nf_200_totals'] = dict(
qs.aggregate(**metrics)
)
return response
然后在你的模板中你像这样遍历结果,你会看到你想要的
{% extends "admin/change_list.html" %}
{% load humanize %}
{% load math_tags %}
{% block content_title %}
<h1> Noise Filter 200 </h1>
{% endblock %}
{% block result_list %}
<style>
.results table {
counter-reset: rowNumber;
}
.results tbody tr{
counter-increment: rowNumber;
}
.results tbody td.rank::before {
content: counter(rowNumber);;
}
</style>
<div style="margin: 10px 0;">Total MCap today: {{ nf_200_totals.total_mcap | intword }}, Total volume today: {{ nf_200_totals.total_volume | intword }} </div>
<div class="results">
<table>
<thead>
<tr>
<th>
<div class="text">
Rank
</div>
</th>
<th>
<div class="text">
<a href="#">Name</a>
</div>
</th>
<th>
<div class="text">
<a href="#">Ticker</a>
</div>
</th>
<th>
<div class="text">
<a href="#">PriceUSD</a>
</div>
</th>
<th>
<div class="text">
<a href="#">Market cap</a>
</div>
</th>
<th>
<div class="text">
<a href="#">Volume</a>
</div>
</th>
<th>
<div class="text">
<a href="#">Change 24</a>
</div>
</th>
</tr>
</thead>
<tbody>
{% for row in nf_200_list %}
<tr class="{% cycle 'row1' 'row2' %}">
<td class="rank"> </td>
<td> {{ row.name }} </td>
<td> {{ row.ticker }} </td>
<td> $ {{ row.priceUSD|to_string }} </td>
<td> {{ row.market_cap | intword }} </td>
<td> {{ row.volume | intword }} </td>
<td> {{ row.change_24h | percent}} </td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
{% block pagination %}{% endblock %}
缺点是你可以在这里使用一些你得到的功能(比如排序)。