获取搜索(过滤)的对象

时间:2018-12-18 06:12:03

标签: python django django-admin

我正在尝试获取某些字段的总和,该数量应该是动态的,具体取决于搜索或过滤器。如何使price_total动态?

class OrderAdmin(ImportExportModelAdmin, admin.ModelAdmin):
    resource_class = OrderResource
    form = OrderForm
    date_hierarchy = "created"
    list_display = ('price_total', 'order_no', 'phone', 'get_directions')
    search_fields = ("document__first_name", "document__last_name", 'order_no', 'user__username')
    readonly_fields = ("dealer", )
    list_filter = ('status', 'payment_type', 'channel', ('buy_time', DateRangeFilter), ('created', DateRangeFilter))

    def price_total(self, obj):
        return obj.__class__.objects.aggregate(Sum('total_price')).get('total_price__sum')

    def order_no(self, obj):
        return obj.order_no

    def phone(self, obj):
        return obj.user.username if obj.user else obj.contact

    def get_directions(self, obj):
        return obj.get_directions()

0 个答案:

没有答案