我试图在Django中显示用户组权限,并以类似矩阵的“Drupal”样式显示它们它可以工作,但是生成查询并在模板中绘制它需要很长时间。有什么方法可以改进我的代码吗?
查看img
up(accomplished),down(views and template.html)
意见:

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>

最佳答案

您的问题是运行了4*200多个查询,每个组合或行和列(权限和组)都有一个查询。通过一个查询获得它们是很有用的。然而,这并不容易,因为ManyToMany模型与Permission模型之间的Group关系的中间模型在django.contrib.auth.models中并不明确。您可以通过Model._meta API获得该模型:

>>> GroupPermissions = Permission._meta.get_field('group').through
>>> GroupPermissions
<class 'django.contrib.auth.models.Group_permissions'>
>>> GroupPermissions._meta.db_table  # the table that you use in the raw query
'auth_group_permissions'

把它们放在一起。更喜欢较长的视图和简单的模板:
更新视图:
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 %}

10-01 10:44