Search code examples
djangoadmindashboardmezzaninedjango-grappelli

Custom admin dashboard displaying specific model instances


I'm creating a website with Django (+ django-grappelli + mezzanine) and I would like to customize my Admin panel to add a new dashboard displaying specific model instances.

Let's say that I have a model :

class Thing(models.Model):
    published = models.BooleanField(default=False)

And several model instances (for example T1, T2, T3 where T1 and T2 are published but not T3) and I would like to have a dashboard displaying a list of all "Thing" instances that are not published (in this case, T3).

Any ideas? Thanks for reading !


Solution

  • 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 !