Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django AdminSite自定义如何编辑sites.py文件?(“适当地”)_Python_Django - Fatal编程技术网

Python Django AdminSite自定义如何编辑sites.py文件?(“适当地”)

Python Django AdminSite自定义如何编辑sites.py文件?(“适当地”),python,django,Python,Django,我当前的目标是在管理界面上显示注册用户的数量,例如,Users Count:{{model.Count}}假设model已经等于用户模型 为了实现这一点,我必须在sites.py中添加一行,该行位于django.contrib.admin中;有没有合适的方法来编辑这个文件?我应该复制整个django.contrib.admin吗 注意:我在venv中编辑了该文件,它可以正常工作,但我需要一种更简洁的方式,即每次修改都位于我的管理应用程序下,而不是在venv中。您需要的是位于本地admin.py文

我当前的目标是在管理界面上显示注册用户的数量,例如,
Users Count:{{model.Count}}
假设model已经等于用户模型

为了实现这一点,我必须在sites.py中添加一行,该行位于django.contrib.admin中;有没有合适的方法来编辑这个文件?我应该复制整个django.contrib.admin吗


注意:我在venv中编辑了该文件,它可以正常工作,但我需要一种更简洁的方式,即每次修改都位于我的管理应用程序下,而不是在venv中。

您需要的是位于本地admin.py文件中的CustomAdminSite。在这种情况下,我必须添加以下函数,包括其所需的导入(从AdminSite部分的“django.contrib.admin.sites.py”复制):

def _build_app_dict(self, request, label=None):
    """
            Build the app dictionary. The optional `label` parameter filters models
            of a specific app.
            """
    app_dict = {}

    if label:
        models = {
            m: m_a for m, m_a in self._registry.items()
            if m._meta.app_label == label
        }
    else:
        models = self._registry

    for model, model_admin in models.items():
        app_label = model._meta.app_label

        has_module_perms = model_admin.has_module_permission(request)
        if not has_module_perms:
            continue

        perms = model_admin.get_model_perms(request)

        # Check whether user has any perm for this module.
        # If so, add the module to the model_list.
        if True not in perms.values():
            continue

        info = (app_label, model._meta.model_name)
        model_dict = {
            'name': capfirst(model._meta.verbose_name_plural),
            'object_name': model._meta.object_name,
            'perms': perms,
            'admin_url': None,
            'add_url': None,
            'count': model.objects.count(), {# !!----ONLY THIS LINE WAS ADDED------- #}
        }
        if perms.get('change') or perms.get('view'):
            model_dict['view_only'] = not perms.get('change')
            try:
                model_dict['admin_url'] = reverse('admin:%s_%s_changelist' % info, current_app=self.name)
            except NoReverseMatch:
                pass
        if perms.get('add'):
            try:
                model_dict['add_url'] = reverse('admin:%s_%s_add' % info, current_app=self.name)
            except NoReverseMatch:
                pass

        if app_label in app_dict:
            app_dict[app_label]['models'].append(model_dict)
        else:
            app_dict[app_label] = {
                'name': apps.get_app_config(app_label).verbose_name,
                'app_label': app_label,
                'app_url': reverse(
                    'admin:app_list',
                    kwargs={'app_label': app_label},
                    current_app=self.name,
                ),
                'has_module_perms': has_module_perms,
                'models': [model_dict],
            }

    if label:
        return app_dict.get(label)
    return app_dict