Skip to content
Advertisement

Django list_display highest value ManyToMany Field

I want to know how I can display the “highest value” from my ManyToMany Field in the admin. This is my models.py file:

JavaScript

In my backend, I have created different types of Degree’s, all with a “ranking”. In my case, the highest degree you can have is “Doctoral Degree”, with a rank of “6”. Backed Admin List for Education

So if a User is creating himself in “Personal” he will have the option to select all the Degree’s he has achieved. But for my Personal list, I just to want to see the highest one, e.g. if the User selects “Bachelor’s Degree” and “Master’s Degree”, the Personal list should only contain “Master’s Degree”, because 5 > 4.

Someone with an idea on how my admin.py file should look like?

JavaScript

Big thanks in advance!

Advertisement

Answer

You can define a method in your ModelAdmin to calculate the maximum degree:

JavaScript

This is however quite inefficient if we want to render a lot of Personals, since that will require an extra query per item.

We can let the database do the work for us with a subquery expression:

JavaScript

This will calculate the name of the degrees in bulk.

Advertisement