Django管理员:如何按照自定义方法排序列 [英] Django admin: how to sort column by custom method
问题描述
class Item(models.Model):
name = models.CharField(max_length = 100,unique = True)
def admin_amount )
total = self.warehouse_set.all()。aggregate(item = Sum('amount'))
return total ['item']
class Warehouse(models 。$)
name = models.CharField(max_length = 100,unique = True)
item = models.ForeignKey('Item',blank = True,null = True)
amount = model.IntegerField()
创建新字段是错误的,但我不能像
admin_amount.admin_order_field ='admin_amount'
我发现类似的问题,但是我遇到一个重写queryset()方法的问题(不能写出像$ code> qs.warehouse_set.all()。annotate(models)。总和( '量')))。有没有办法适应这个解决方案给我或在我的情况下,还有另一个解决方案?
a href =https://stackoverflow.com/questions/2168475/django-admin-how-to-sort-by-one-of-the-custom-list-display-fields-that-has-no-d >链接的问题(以及建议的编辑),底部应该为您的示例。原则是使用annotate将附加数据从子查询添加到返回的QuerySet。在这种情况下,仓库中金额的 Sum
。
接下来,添加包装函数 amount_in_warehouses
为每行获取此值,并告诉管理员显示这个列表 list_display =('amount_in_warehouses',)
,以及排序 amount_in_warehouses.admin_order_field ='amount_in_warehouses'
。
class ItemAdmin (admin.ModelAdmin):
list_display =('amount_in_warehouses')
name = models.CharField(max_length = 100,unique = True)
def queryset(self,request )
qs = super(ItemAdmin,self).queryset(request)
qs = qs.annotate(models.Sum('warehouse__amount'))
return qs
def amount_in_warehouses(self,obj):
return obj.warehouse__amount__sum
amount_in_warehouses.admin_order_field ='amount_in_warehouses'
class Item(models.Model):
name = models.CharField(max_length=100, unique=True)
def admin_amount(self):
total = self.warehouse_set.all().aggregate(item=Sum('amount'))
return total['item']
class Warehouse(models.Model):
name = models.CharField(max_length=100, unique=True)
item = models.ForeignKey('Item', blank=True, null=True)
amount = models.IntegerField()
create new field is wrong, but i cant do something like
admin_amount.admin_order_field = 'admin_amount'
I found similar question but i encountered a problem with rewriting queryset() method(cant write something like qs.warehouse_set.all().annotate(models.Sum('amount'))
). Is there any way to adapt this solution for me or in my case, there is another solution?
Using the code in the linked question (and the suggested edit), the bottom should do it for your example. The principle is to use annotate to add additional data from a sub-query to the returned QuerySet. In this case the Sum
of the amounts in warehouses.
Next you add a wrapper function amount_in_warehouses
to get this value out for each row, and tell the admin to show this for listing list_display = ('amount_in_warehouses',)
, and sort on it amount_in_warehouses.admin_order_field = 'amount_in_warehouses'
.
class ItemAdmin(admin.ModelAdmin):
list_display = ('amount_in_warehouses',)
name = models.CharField(max_length=100, unique=True)
def queryset(self, request):
qs = super(ItemAdmin, self).queryset(request)
qs = qs.annotate(models.Sum('warehouse__amount'))
return qs
def amount_in_warehouses(self, obj):
return obj.warehouse__amount__sum
amount_in_warehouses.admin_order_field = 'amount_in_warehouses'
这篇关于Django管理员:如何按照自定义方法排序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!