modeladmin中代理模型的用户权限

时间:2011-11-11 16:28:09

标签: django django-admin

使用代理模型时:

class Uf(models.Model):
...

class CustomUf(Uf):
    class Meta:
        proxy = True

class CustomUfAdmin(admin.ModelAdmin)

admin.site.register(CustomUf, CustomUfAdmin)

似乎只有超级用户才能通过管理站点访问CustomUf ...我无法弄清楚如何向常规用户授予CustomUf权限......

4 个答案:

答案 0 :(得分:12)

好的,克里斯关于内容类型的评论给了我提示......

我错误地在“admin.py”中定义代理对象。这样,您必须使用superadmin才能访问它。

如果我在models.py中定义代理对象,则会显示内容类型,一切正常......

答案 1 :(得分:4)

您需要再次运行syncdb,以便可以选择新的内容类型。

答案 2 :(得分:4)

请参阅此相关的Django问题:#11154

您可以通过手动将行添加到'auth_permission'表中来解决此问题,如:

INSERT INTO "auth_permission" ("name","content_type_id","codename") 
    VALUES ('Can add proxy model name',{content_type_id},'add_proxy_model_name');

其中content type id是relavent内容类型的整数id。

答案 3 :(得分:1)

我意识到这个问题已经暂时关闭了,但是我会分享对我有用的东西,以防它可以帮助别人。

事实证明,即使我创建的代理模型的权限列在父应用程序下,即使我授予非超级用户所有权限,仍然拒绝通过管理员访问我的代理模型。

如果您想避免raw SQL并在Python中编写修补程序脚本,则必须解决已知的Django错误(https://code.djangoproject.com/ticket/11154)并连接到post_syncdb信号以正确创建权限代理模型。以下代码根据该主题的一些评论从https://djangosnippets.org/snippets/2677/修改。

我把它放在myapp / models.py中,它包含我的代理模型。从理论上讲,这可以存放在INSTALLED_APPS之后的任何django.contrib.contenttypes中,因为需要在update_contenttypes处理程序注册post_syncdb信号后加载它,以便我们断开它。< / p>

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)