0

At Django Admin, we can use "list_display" to choose which column to show.

I want to customize Django admin so that we can decide which column to show dynamically (Not using "list_display) I want the user to be able to use something like a checkbox. Again, not by changing Python codes.

I tried the following but it seems nothing to do with the display of listing.

Dynamic fields in Django Admin

It seems I need to override/extend some Django template using JavaScript but I am not so sure.

Any comment would be greatly appreciated. (I much prefer to implement it in admin rather than outside of admin, though.)

Many thanks,

Cheolsoon Im
  • 716
  • 1
  • 6
  • 11
  • 1
    For altering which fields are displayed dynamically, you would implement this by overriding the get_fields or get_fieldsets methods inside the admin.py manager (like the post you linked). If you would like to change the fields type (square checkbox to radio-button style) then you would need to override the fields widget with a django widget or a custom created widget. This would be done by using formfield_overrides – plum 0 Dec 29 '20 at 15:38
  • Thanks so much. When I tried the link I posted it changes only the other screen(the one you can see when you click the specific record) (not the list display). Can you tell me why? I will dig in more though. Thanks again. – Cheolsoon Im Dec 29 '20 at 15:50
  • 1
    Also, take a look at django proxy models. They can accomplish what you need because they rely on the same db table but can implement different logic (so different admin's list_display fields too). It's easier in my opinion, you can hide or show the models in the admin interface only for a certain kind of user just with django's built in permissions at that point. Much more safe and straightforward. https://docs.djangoproject.com/en/3.1/topics/db/models/#proxy-models – Manuel Fedele Dec 29 '20 at 15:51
  • 1
    My apologies, I misread the question with my above comment! You are correct, the get_fieldsets method will only override the fields displayed when adding a new object or modifying an existing one. What you are looking to override is the `get_list_display()` method. This will allow you to control the columns you show prior to editing. You can read more about it here: https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display – plum 0 Dec 29 '20 at 15:59
  • Thanks so much to both of you. @plum 0, it works quite well. :) To Miles Davis, I will try out what you told me. All the best in 2021! :) – Cheolsoon Im Dec 29 '20 at 23:24

0 Answers0