I am using Django 1.8.8
.
I have a lot of 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 setup 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 setup my admin page as follows:
Have one link on the top page/admin
, "MainModels" which directs me to another page where I can select from a list of all derived models of MyModel
, i.e. [MyModel1, MyModel2, ....MyMoneyN]
which lets me edit the selected derived model. MyModel#
.
The problem with the code above is it creates a top level link for each numbered model.
You're really best off using something like Grapelli or another custom admin replacement. You can set custom pages with different sets of models, adjust hidden / shown by default, etc. Trying to do this with vanilla admin is going to 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.*',),
))
You need your own changelist to do this:
class MyChangeList(ChangeList):
def url_for_result(self, result):
link = your_function_to_create_link()
return link
Then use your Changelist
in ModelAdmin
:
class MyModelAdmin(admin.ModelAdmin):
def get_changelist(self, request, **kwargs):
return MyChangeList
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With