Question:
Disabling default ordering in Django admin panel

Problem:

I'm trying to use 'liczba_glosow' field for ordering in Django admin panel. The problem is that the model itself has ordering by "numer_na_liscie" in Meta method and admin uses default ordering and than 'liczba_glosow'. I would like admin to use ONLY order_by("-_liczba_glosow") and bypass default. I can't use simple ordering="liczba_glosow" because this field is created only for admin display purpose and it's not part of a model itself.


class KandydatDoSejmuAdmin(admin.ModelAdmin):

    list_display = ("imie_nazwisko", "numer_na_liscie", "okreg_wyborczy", "komitet_wyborczy", "liczba_glosow")

    raw_id_fields = ("okreg_wyborczy", "komitet_wyborczy")

    list_filter = ("komitet_wyborczy__nazwa", "oddanyglos__kod_pocztowy__wojewodztwo", "okreg_wyborczy__nazwa",)

    search_fields = (

        "imie_nazwisko", "okreg_wyborczy__nazwa", "komitet_wyborczy__nazwa", "oddanyglos__kod_pocztowy__wojewodztwo")

    list_per_page = 10


    def liczba_glosow(self, obj):

        return obj.oddanyglos_set.count()


    liczba_glosow.admin_order_field = "_liczba_glosow"


    def get_queryset(self, request):

        qs = super().get_queryset(request)

        return qs.annotate(

            _liczba_glosow=Count("oddanyglos__kandydat")

        ).order_by("-_liczba_glosow")



According to Django docs:


Set ordering to specify how lists of objects should be ordered in the Django admin views. This should be a list or tuple in the same format as a model’s ordering parameter. If this isn’t provided, the Django admin will use the model’s default ordering.


According to the above provision, any attempt to disable the default sorting like ordering=None, get_ordering() returning None or empty list doesn't work.

Is there an option to override the default sorting in the admin panel without interfering with the model? I need the default sorting of the model in other parts of the application, so I would prefer to leave it.


Solution:

You can set it to your custom ordering with:


class KandydatDoSejmuAdmin(admin.ModelAdmin):

    list_display = (

        'imie_nazwisko',

        'numer_na_liscie',

        'okreg_wyborczy',

        'komitet_wyborczy',

        'liczba_glosow',

    )

    raw_id_fields = ('okreg_wyborczy', 'komitet_wyborczy')

    list_filter = (

        'komitet_wyborczy__nazwa',

        'oddanyglos__kod_pocztowy__wojewodztwo',

        'okreg_wyborczy__nazwa',

    )

    search_fields = (

        'imie_nazwisko',

        'okreg_wyborczy__nazwa',

        'komitet_wyborczy__nazwa',

        'oddanyglos__kod_pocztowy__wojewodztwo',

    )

    list_per_page = 10

    ordering = '-_liczba_glosow'


    def liczba_glosow(self, obj):

        return obj.oddanyglos_set.count()


    liczba_glosow.admin_order_field = '_liczba_glosow'


    def get_queryset(self, request):

        return (

            super()

            .get_queryset(request)

            .annotate(_liczba_glosow=Count('oddanyglos__kandydat'))

        )


we thus inject an expression in get_queryset, and then we use >.ordering [Django-doc] to use that as ordering.


Answered by: >Willem Van Onsem

Credit: >Stackoverflow


Suggested reads:

>What is data binding in Angular?

>What is microservice architecture, and why is it better than monolithic architecture?

>What is pipe in Angular?

>What makes Python 'flow' with HTML nicely as compared to PHP?

>What to do when MQTT terminates an infinite loop while subscribing to a topic in Python?

>Creating custom required rule - Laravel validation

>How to configure transfers for different accounts in stripe with laravel?

>Laravel Query Solved: Laravel withOnly not restricting the query


Ritu Singh

Ritu Singh

Submit
0 Answers