Django - 如何让ModelAdmin类出现在可用权限中

0 投票
5 回答
1255 浏览
提问于 2025-04-16 14:43

我做了一个简单的Django应用程序。我有一个模型叫“Visitor”(访客)。我的目标是让Django后台显示两个表格,一个是所有访客的列表,另一个是今天的访客列表。

我按照这些说明,用下面的代码把一切都搞定了。但是我遇到的问题是,在后台编辑用户组时,找不到VisitorExpectedTodayProxy这个选项,想知道怎么才能让它显示出来。

Models.py

class Visitor(models.Model):
    visit_datetime = models.DateTimeField(null=True)
    visitor_name = models.CharField(max_length=500)

#Make dummy models for different object views in admin interface
class VisitorExpectedTodayProxy(Visitor):
    class Meta:
        proxy=True
        verbose_name = "Visitor"
        verbose_name_plural = "Today's Visitors and Regular Visitors"

更新:

我确实运行了syncdb命令,但在后台网站上还是看不到它。syncdb的结果是:

$ python manage.py syncdb
Syncing...
No fixtures found.

Synced:
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.sites
 > django.contrib.messages
 > django.contrib.admin
 > south

5 个回答

1

我知道这个问题之前已经关闭了,但我想分享一下我找到的解决办法,希望能对其他人有所帮助。

其实,尽管我创建的代理模型的权限在运行 syncdb 后显示在父应用下,并且我给我的非超级用户分配了所有权限,但通过管理后台访问我的代理模型时,仍然被拒绝了。

虽然我没有尝试上面提到的基于 SQL 的解决方案,但在 Django 层面上修复这个问题对我有效。你需要绕过一个已知的 Django 错误(可以查看这个链接了解更多:https://code.djangoproject.com/ticket/11154),并连接到 post_syncdb 信号,以正确创建代理模型的权限。下面的代码是根据一些评论修改自这个链接:https://djangosnippets.org/snippets/2677/

我把这段代码放在了我的应用的 models.py 文件中,这个文件里包含了我的代理模型。理论上,这段代码可以放在你的任何 INSTALLED_APPS 中,只要是在 django.contrib.contenttypes 之后,因为它需要在 update_contenttypes 处理程序注册到 post_syncdb 信号后加载,这样我们才能断开连接。

def create_proxy_permissions(app, created_models, verbosity, **kwargs):
    """
    Creates permissions for proxy models which are not created automatically
    by 'django.contrib.auth.management.create_permissions'.
    See https://code.djangoproject.com/ticket/11154
    Source: https://djangosnippets.org/snippets/2677/

    Since we can't rely on 'get_for_model' we must fallback to
    'get_by_natural_key'. However, this method doesn't automatically create
    missing 'ContentType' so we must ensure all the models' 'ContentType's are
    created before running this method. We do so by un-registering the
    'update_contenttypes' 'post_syncdb' signal and calling it in here just
    before doing everything.
    """
    update_contenttypes(app, created_models, verbosity, **kwargs)
    app_models = models.get_models(app)
    # The permissions we're looking for as (content_type, (codename, name))
    searched_perms = list()
    # The codenames and ctypes that should exist.
    ctypes = set()
    for model in app_models:
        opts = model._meta
        if opts.proxy:
            # Can't use 'get_for_model' here since it doesn't return
            # the correct 'ContentType' for proxy models.
            # See https://code.djangoproject.com/ticket/17648
            app_label, model = opts.app_label, opts.object_name.lower()
            ctype = ContentType.objects.get_by_natural_key(app_label, model)
            ctypes.add(ctype)
            for perm in _get_all_permissions(opts, ctype):
                searched_perms.append((ctype, perm))

    # Find all the Permissions that have a content_type for a model we're
    # looking for. We don't need to check for codenames since we already have
    # a list of the ones we're going to create.
    all_perms = set(Permission.objects.filter(
        content_type__in=ctypes,
    ).values_list(
        "content_type", "codename"
    ))

    objs = [
        Permission(codename=codename, name=name, content_type=ctype)
        for ctype, (codename, name) in searched_perms
        if (ctype.pk, codename) not in all_perms
    ]
    Permission.objects.bulk_create(objs)
    if verbosity >= 2:
        for obj in objs:
            sys.stdout.write("Adding permission '%s'" % obj)


models.signals.post_syncdb.connect(create_proxy_permissions)
# See 'create_proxy_permissions' docstring to understand why we un-register
# this signal handler.
models.signals.post_syncdb.disconnect(update_contenttypes)
1

这是我用来手动输入代理对象的内容类型条目的一个脚本,然后为它们创建新的权限。

这个脚本只是生成了 SQL 语句,你需要在 MySQL 中运行这些语句。运行后,它们就会出现在用户的权限列表中,你需要把它们添加进去。这里面有些命名规则可能不太好理解。

your_models=['proxy model',
            ]

for model in models:
    model_nospace = model.replace(' ','')
    sql = 'insert into django_content_type (name, app_label, model) values     ("%s","<<app_name>>","%s");'%(model,model_nospace)
    print sql
    for kind, permname in [('Can add','add_%s'%model_nospace),
                          ('Can change','change_%s'%model_nospace),
                          ('Can delete','delete_%s'%model_nospace),]:
        sql = 'insert into auth_permission (name, content_type_id, codename) values ("%s     %s",(select id from django_content_type where name="%s"),"%s");'%    (kind,model,model,permname)
        print sql
-2

我在这里记录了我解决这个问题的办法。

撰写回答