dev9
dev9

Reputation: 2382

How to sort by many custom methods in Django Admin

I want to be able to sort by several custom methods in Django Admin. This question provides solution for one method only.

I tried to modify it:

from django.db import models

class CustomerAdmin(admin.ModelAdmin):
    list_display = ('number_of_orders','number_of_somevalue') # added field

    def queryset(self, request):
        qs = super(CustomerAdmin, self).queryset(request)
        qs = qs.annotate(models.Count('order'))
        qs = qs.annotate(models.Count('somevalue')) # added line
        return qs

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

    def number_of_somevalue(self, obj): # added method
        return obj.somevalue__count
    number_of_somevalue.admin_order_field = 'somevalue__count'

and it works incorrectly. It seems that it multiplies the count values instead of counting them separately.

Example:

I have 2 orders and 2 somevalues, but in the panel I see 4 orders and 4 somevalues.

Adding another method with yet another value makes it 8 (2*2*2).

How can I fix it?

Upvotes: 1

Views: 1306

Answers (1)

pipesalazar
pipesalazar

Reputation: 11

You can try this to sort by many custom methods (Tested):

from django.db.models import Count

class CustomerAdmin(admin.ModelAdmin):

    # The list display must contain the functions that calculate values
    list_display = ('number_of_orders','number_of_somevalue') # added field

    # Overwrite queryset in model admin
    def queryset(self, request):
        qs = super(CustomerAdmin, self).queryset(request)
        # The query have to return multiple annotation, for this use distinct=True in the Count function
        qs = qs.annotate(number_orders = Count('order', distinct=True)).annotate(number_somevalue = Count('somevalue',distinct=True)) 
        return qs

    # This function return the new field calculated in queryset (number_orders)
    def number_of_orders(self, obj):
        return obj.number_orders

    number_of_orders.admin_order_field = 'numberorders' # sortable new column

    # And this one will return the another field calculated (number_somevalue)
    def number_of_somevalue(self, obj): # added method
        return obj.number_somevalue

    number_of_somevalue.admin_order_field = 'number_somevalue'# sortable new column

Upvotes: 1

Related Questions