Unknown Username
Unknown Username

Reputation: 63

Get model's ModelAdmin "list_display" fields

Consider a model called Person which has four fields:

I registered this model on the admin site. In the ModelAdmin class, I specified the list_display` fields like so:

list_display = ["f_name", "l_name", "something_else"]

In the view.py file, I called the model object using the apps.get_model() function from django.apps. Now that I have this model object, I want to retrieve the list_display fields through this model object. How would I be able do that?

Thanks for reading this awfully presented question and your time, I really appreciate any help.

EDIT : I would also like to know how I store the data in a list according to the list_display fields from a queryset. For example:

represented_data = [['John', "Snow", "He died"], ["Arya", "Not Snow", "Killed the king"]]

Upvotes: 0

Views: 459

Answers (1)

kaajavi
kaajavi

Reputation: 560

This is an example to get ModelAdmin from Model

from django.contrib import admin
...

YourModel = apps.get_model('your_app_name','your_model')
your_list_display = admin.site._registry[YourModel].list_display
...

and, to get data as a list of lists, you can use the next code:

represented_data = list(YourModel.objects.values_list(*your_list_display))

Upvotes: 1

Related Questions