0

I need django(2.1.1) admin to sort changelist page of a model by a calculated value. This is what I have in the admin class:

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        qs = qs.annotate(
            virtual_sale=ExpressionWrapper(Func(F('sales_total'), Value('virtual'), Value('count'),function='jsonb_extract_path_text'),output_field=IntegerField())
            )
        return qs

    def virtual_sale(self, obj):
        return obj.virtual_sale
    virtual_sale.admin_order_field = 'virtual_sale'

FYI, the annotation is to extract an IntegerField "virtual_sale" from the JSONField "sales_total".

But when sorting, it is sorted in way as virtual_sale values are strings, I mean its is sorted this way: [0,1,11,2,330,5]

What is the problem and how can I fix it?

javad
  • 29
  • 2

1 Answers1

0

Finally, I understood that I should Cast the fields into IntegerField.

javad
  • 29
  • 2