Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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中优化代码-查看多个矩阵_Python_Django_Django Templates_Django Views - Fatal编程技术网

Python 在Django中优化代码-查看多个矩阵

Python 在Django中优化代码-查看多个矩阵,python,django,django-templates,django-views,Python,Django,Django Templates,Django Views,我试图在Django中显示用户组权限,并以类似矩阵的“Drupal”样式显示它们。它可以工作,但是进行查询并在模板中绘制它需要太长时间。有什么方法可以改进我的代码吗? 查看img 观点: def GroupPermissionsView(request): title = "Groups Permissions" groups = Group.objects.all() permissions = Permission.objects.all() conte

我试图在Django中显示用户组权限,并以类似矩阵的“Drupal”样式显示它们。它可以工作,但是进行查询并在模板中绘制它需要太长时间。有什么方法可以改进我的代码吗? 查看img

观点:

def GroupPermissionsView(request):

    title = "Groups Permissions"
    groups = Group.objects.all()
    permissions = Permission.objects.all()

    context = Context({
        'title': title,
        'groups': groups,
        'permissions': permissions,
    })
    return render(
        request,
        'forms_permissions.html',
        context
    )
模板:

<table class="table table-striped table-inverse table-responsive table-bordered">

  <thead>
        <tr>
            <th>Permission</th>
            {% for group in groups %}
                <th>{{ group.name }}</th>
            {% endfor %}
        </tr>
  </thead>

  <tbody>
        {% for permission in permissions %}
        <tr>
        <td><b>{{permission.name}}<b></td>
        {% for group in groups %}
             {% if permission in group.permissions.all %}
                        <td><input type="checkbox" name="" checked="checked"></input></td>
            {% else %}
                        <td><input type="checkbox" ></input></td>
            {% endif %}
        {% endfor %}

        </tr>
        {% endfor %}
  </tbody>
</table>

许可
{组%中的组的%s}
{{group.name}
{%endfor%}
{permissions%%中的权限为%s}
{{permission.name}
{组%中的组的%s}
{%if group.permissions.all%中的权限}
{%else%}
{%endif%}
{%endfor%}
{%endfor%}

您的问题是您运行了4*200多个查询,每个组合或行和列(权限和组)都有一个查询。通过一个查询获取所有这些信息是很有用的。然而,这并不容易,因为
权限
模型之间的
多个
关系的中间模型在
django.contrib.auth.models
中并不明确。您可以通过以下方式获得该模型:

更新模板

{% for permission, cells in permission_group_table.items %}
    <tr><td><b>{{permission.name}}<b></td>
    {% for cell in cells %}
        {% if cell %}
            <td><input type="checkbox" name="" checked="checked"></input></td>
        {% else %}
            <td><input type="checkbox" ></input></td>
        {%  endif %}
    {% endfor %}
    </tr>
{% endfor %}
{%表示权限,权限组中的单元格\表项%}
{{permission.name}
{单元格%中的单元格的百分比}
{%if单元格%}
{%else%}
{%endif%}
{%endfor%}
{%endfor%}

您有多少组和权限?组4任务更多200兄弟非常感谢我尝试了它,它很有效,感谢您的帮助,不让它在尝试中死去,少一个初学者如果您同意,我将编辑您的问题,使其更清晰,对其他人更有用。这通常是你(OP-原创海报)的附加值,可以免费获得答案,但不会失去工作。看看编辑历史,也许你更喜欢原始的措辞
from collections import OrderedDict

GroupPermissions = Permission._meta.get_field('group').through
groups = Group.objects.all()
permissions = Permission.objects.all()
permission_group_set = set()
for x in GroupPermissions.objects.all():
    permission_group_set.add((x.permission_id, x.group_id))
# row title and cells for every permission
permission_group_table = OrderedDict([
    (permission, [(permission.id, group.id) in permission_group_set for group in groups])
    for permission in permissions
])

context = Context({
    'title': title,
    'groups': groups,
    'permission_group_table': permission_group_table,
})
{% for permission, cells in permission_group_table.items %}
    <tr><td><b>{{permission.name}}<b></td>
    {% for cell in cells %}
        {% if cell %}
            <td><input type="checkbox" name="" checked="checked"></input></td>
        {% else %}
            <td><input type="checkbox" ></input></td>
        {%  endif %}
    {% endfor %}
    </tr>
{% endfor %}