我有以下django admin exceprt:
class TagAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
list_display = ['title', 'total_videos', 'total_active_videos', 'weight', 'status']
search_fields = ['title']
list_filter = ['status']
ordering = ['-weight']
def queryset(self, request):
return Tag.objects.annotate(total_videos_order=Count('video'))
def total_videos(self, obj):
return obj.total_videos_order
total_videos.admin_order_field = 'total_videos_order'
def total_active_videos(self, obj):
return u'%s' % Video.objects.filter(tags=obj, status=Video.STATUS_ACTIVE).count()
现在一切正常,total_videos和total_active_videos都显示正确的信息。如果单击表标题,total_videos也是可排序的。但是,如果我尝试将代码更改为:
ordering = ['total_videos_order']
我收到错误:
TagAdmin.ordering [0]'是指字段" total_videos_order'模特' videos.Tag'。
中缺少这些内容
如果只是" total_videos"作为一种解决方法,我可以忽略它,只需点击标题,但它困扰我的原因,如果它通过单击默认顺序排序无法设置,当字段显然存在?有什么想法?
问题的第二部分是否可以在过滤字段上添加排序" total_active_videos"。根据我的理解,它不可能用条件注释,所以它不能像它完成的那样完成" total_videos"但还有其他解决方案吗?
谢谢!