[Answered ]-Custom admin dashboard displaying specific model instances

2đź‘Ť

âś…

Ok I’ve found a solution. Here are the guidelines :

Mezzanine allows the users to customize their dashboard giving a function and registering it as inclusion tag.

Documentation : http://mezzanine.jupo.org/docs/admin-customization.html -> Dashboard https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/#inclusion-tags

To implement such a function, you need to follow these steps :

1) Add a templatetags folder in your application (don’t forget the __init__.py file) and create a file called “your_tags.py” inside of this package.

2) In this new file, add a function to provide data to the new dashboard you want to add in the Dashboard panel. It could look like this :

from mezzanine import template
from your_app.models import Thing

register = template.Library()
@register.inclusion_tag('unpublished_things.html')
def show_unpublished_things():
    plugins = Thing.objects.filter(published=False)
    return {'things':things}

3) Then you need to create the “unpublished_things.html” file used in the inclusion tag, so for example create such a file in the templates folder of your application. The file could look like this (assuming there is a “get_admin_url” function in the Thing model) :

{% load i18n %}

<div class="group-collapsible">
    <div class="module">
    <table>
        <caption>Unpublished things</caption>
        {% for thing in things %}
        <tr>
            <th scope="row" width="100%"><a
                    href="{{ thing.get_admin_url }}">{{ thing.name }}</a></th>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        {% endfor %}
    </table>
    </div>
</div>

4) To finish, you just need to add the following in your local_settings.py (or settings.py) :

DASHBOARD_TAGS = (
    ("your_tags.show_unpublished_things", "mezzanine_tags.app_list"),
    ("comment_tags.recent_comments",),
    ("mezzanine_tags.recent_actions",),
)

This configuration will automatically add the generated stuff provided by the function “show_unpublished_things” at the top of the first row, in the Dashboard of your admin panel.

Don’t forget to restart your server if you get an error !

👤Jerome

0đź‘Ť

It sounds like what you want is a ModelAdmin with a custom list_filter, as seen here: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter

For example, in the same folder as your models.py, you would create an admin.py which looked something like:

from django.contrib import admin

from .models import Thing


class ThingAdmin(admin.ModelAdmin):
    list_filter = ('published',)

admin.site.register(Thing, ThingAdmin)

This will give you a new section in your admin site for your Thing objects, and allow you to filter them by whether or not they have been published.

👤Joey Wilhelm

Leave a comment