Django在管理界面上显示对象计数

时间:2016-06-01 21:47:45

标签: django django-admin django-rest-framework

我在dashboard.py文件的帮助下修改了仪表板。我在其中添加了一个名为“我的小工具”的自定义模块/小部件。 (避免创造自己,而只是作为一个孩子附加到特定的django应用程序)。代码如下:

  # append an app list module for "Administration"
    self.children.append(modules.AppList(
        _('Administration'),
        models=('django.contrib.*',),
    ))

    self.children.append(modules.AppList(
        _('My widget'),
        models=('flights.*',),
        children=[

        ]
    ))

航班是我的django应用程序,它有三个型号。仪表板如下所示:enter image description here

在这个小部件上,我想显示今天登录的用户数。 我可以通过以下代码片段获得计数本身(不是在管理界面上,而是通过可通过URL访问的视图用于测试目的):  User.objects.filter(last_login__startswith=timezone.now().date()).count()

如何在此小部件上显示此类数据?

整个dashboard.py:

"""
This file was generated with the customdashboard management command, it
contains the two classes for the main dashboard and app index dashboard.
You can customize these classes as you want.

To activate your index dashboard add the following to your settings.py::
ADMIN_TOOLS_INDEX_DASHBOARD = 'api.dashboard.CustomIndexDashboard'

And to activate the app index dashboard::
ADMIN_TOOLS_APP_INDEX_DASHBOARD = 'api.dashboard.CustomAppIndexDashboard'
"""

from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse

from admin_tools.dashboard import modules, Dashboard, AppIndexDashboard
from admin_tools.utils import get_admin_site_name
from admin_tools_stats.modules import DashboardCharts, get_active_graph

class CustomIndexDashboard(Dashboard):
 """
Custom index dashboard for api.
"""
columns = 3

def init_with_context(self, context):
    site_name = get_admin_site_name(context)
    # append a link list module for "quick links"
    self.children.append(modules.LinkList(
        _('Quick links'),
        layout='inline',
        draggable=False,
        deletable=False,
        collapsible=False,
        children=[
            [_('Return to site'), '/'],
            [_('Change password'),
             reverse('%s:password_change' % site_name)],
            [_('Log out'), reverse('%s:logout' % site_name)],
        ]
    ))

    graph_list = get_active_graph()
    for i in graph_list:
        kwargs = {}
        kwargs['require_chart_jscss'] = True
        kwargs['graph_key'] = i.graph_key

        if context['request'].POST.get('select_box_' + i.graph_key):
            kwargs['select_box_' + i.graph_key] =   context['request'].POST['select_box_' + i.graph_key]
            self.children.append(DashboardCharts(**kwargs))

    # append an app list module for "Applications"
    self.children.append(modules.AppList(
        _('All Applications'),
        exclude=('django.contrib.*',),
    ))

    # append an app list module
    self.children.append(modules.AppList(
        _('Dashboard Stats Settings'),
        models=('admin_tools_stats.*', ),
    ))

    # append an app list module for "Administration"
    self.children.append(modules.AppList(
        _('Administration'),
        models=('django.contrib.*',),
    ))

    self.children.append(modules.AppList(
        _('My widget'),
        models=('flights.*',),
        content='test content',
        children=[

        ]
    ))

    # append a recent actions module
    self.children.append(modules.RecentActions(_('Recent Actions'), 4))        


    # append another link list module for "support".
    self.children.append(modules.LinkList(
        _('Support'),
        children=[
            {
                'title': _('Django documentation'),
                'url': 'http://docs.djangoproject.com/',
                'external': True,
            },
            {
                'title': _('Django "django-users" mailing list'),
                'url': 'http://groups.google.com/group/django-users',
                'external': True,
            },
            {
                'title': _('Django irc channel'),
                'url': 'irc://irc.freenode.net/django',
                'external': True,
            },
        ]
    ))




class CustomAppIndexDashboard(AppIndexDashboard):
"""
Custom app index dashboard for api.
"""

# we disable title because its redundant with the model list module
title = ''

def __init__(self, *args, **kwargs):
    AppIndexDashboard.__init__(self, *args, **kwargs)

    # append a model list module and a recent actions module
    self.children += [
        modules.ModelList(self.app_title, self.models),
        modules.RecentActions(
            _('Recent Actions'),
            include_list=self.get_app_content_types(),
            limit=5
        )
    ]

def init_with_context(self, context):
    """
    Use this method if you need to access the request context.
    """
    return super(CustomAppIndexDashboard, self).init_with_context(context)

0 个答案:

没有答案