网站建设的维护与更新,网站排名软件包年,徐州网站营销,wordpress get_post_category_id通常#xff0c;Django会为模型属性字段#xff0c;自动添加排序功能。当你添加计算字段时#xff0c;Django不知道如何执行order_by#xff0c;因此它不会在该字段上添加排序功能。 
如果要在计算字段上添加排序#xff0c;则必须告诉Django需要排序的内容。你可以通过在…通常Django会为模型属性字段自动添加排序功能。当你添加计算字段时Django不知道如何执行order_by因此它不会在该字段上添加排序功能。 
如果要在计算字段上添加排序则必须告诉Django需要排序的内容。你可以通过在在计算字段方法中设置admin_order_field属性来执行此操作 。 
以OriginAdmin为例添加以下代码 
hero_count.admin_order_field  _hero_count
villain_count.admin_order_field  _villain_count 
完整代码如下 
admin.register(Origin)
class OriginAdmin(admin.ModelAdmin):list_display  (name, hero_count, villain_count)def get_queryset(self, request):queryset  super().get_queryset(request)queryset  queryset.annotate(_hero_countCount(hero, distinctTrue),_villain_countCount(villain, distinctTrue),)return querysetdef hero_count(self, obj):return obj._hero_countdef villain_count(self, obj):return obj._villain_counthero_count.admin_order_field  _hero_countvillain_count.admin_order_field  _villain_count 
显示效果 
前 后点击抬头标签显示字段排序