Django Admin: Show List of Models

I am using Django 1.8.8 .

I have many models defined as such:

 class MainModel(models.Model): value = models.IntegerField(null=False, editable=True, default=20) dt_modified = models.DateTimeField(null=True, auto_now=True) class MyModel1(MainModel, models.Model): name = models.CharField(null=False, editable=False, max_length=50) class MyModel2(MainModel, models.Model): foo = models.CharField(null=False, editable=False, max_length=50) 

My admin page is currently set up as such,

 class MyModelAdmin(admin.ModelAdmin): list_display = ('value', 'dt_modified') search_fields = ['value'] date_hierarchy = 'dt_modified' models = [MyModel1, MyModel2] admin.site.register(models, MyModelAdmin) 

I want to configure my admin page as follows: You have one link on the main /admin page, "MainModels", which directs me to another page where I can select from a list of all MyModel derived models, i.e. [MyModel1, MyModel2, ....MyMoneyN] , which allows me to edit the selected derived model. MyModel# .

The problem with the above code is to create a top level link for each numbered model.

+7
django django-admin
source share
2 answers

To do this, you need your own list of changes:

 class MyChangeList(ChangeList): def url_for_result(self, result): link = your_function_to_create_link() return link 

Then use Changelist in ModelAdmin :

 class MyModelAdmin(admin.ModelAdmin): def get_changelist(self, request, **kwargs): return MyChangeList 
+1
source share

Your best bet is to use something like Grapelli or another custom admin replacement. You can set up custom pages with different sets of models, set up hidden / shown by default, etc. Attempting to do this with the help of a vanilla administrator will be problematic.

Here is an example of something close to what you want to do with grappelli

 from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ from grappelli.dashboard import modules, Dashboard class MyDashboard(Dashboard): def __init__(self, **kwargs): Dashboard.__init__(self, **kwargs) # append an app list module for "Applications" self.children.append(modules.AppList( title=_('Applications'), column=1, css_classes=('grp-collapse grp-closed',), collapsible=True, exclude=('django.contrib.*',), )) 
+1
source share

All Articles