Django modeladmin中代理模型的用户权限

Django modeladmin中代理模型的用户权限,django,django-admin,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的权限授予普通用户…您需要再次运行syncdb,

使用代理模型时:

class Uf(models.Model):
...

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

class CustomUfAdmin(admin.ModelAdmin)

admin.site.register(CustomUf, CustomUfAdmin)

似乎只有超级用户才能通过管理员网站访问CustomUf。。。我不知道如何将CustomUf的权限授予普通用户…

您需要再次运行syncdb,以便能够获取新的内容类型。

好的,Chris关于内容类型的评论给了我提示

我错误地在“admin.py”中定义了代理对象。这样,您必须是超级管理员才能访问它


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

请参阅此相关Django问题:

您可以通过手动将行添加到“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是相关内容类型的整数id。

我意识到这个问题不久前已经解决了,但我正在分享对我有用的东西,以防它可能会帮助其他人

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

如果要避免使用Python编写修复程序并编写脚本,则必须绕过已知的Django bug(),并连接到
post\u syncdb
信号,以正确创建代理模型的权限。下面的代码是根据该线程上的一些注释修改的

我把它放在保存代理模型的myapp/models.py中。理论上,这可以在
django.contrib.contenttypes
之后的任何
INSTALLED\u应用程序中存在,因为它需要在
update\u contenttypes
处理程序注册
post\u 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)

对不起,但它不起作用。。。当我向用户授予超级用户权限时,他可以访问CustomUfAdmin视图,但如果没有这些权限,它将不起作用……如果您使用South,这将不起作用,因为syncdb会跳过任何South管理的应用程序。@Blaise您是否使用South管理该应用程序?如果是这样,您需要手动强制南方触发
post\u syncdb
,选中是,这最终对我有效。必须向南才能触发后同步。这在Django 1.3中对我有效。将代理模型移动到models.py中,重新运行syncdb。新权限!被接受的答案对我不起作用,但我可能把appname/modelname搞砸了。这成功了。我制作了一个脚本来手动完成这项工作。当代理模型出现在基础模型以外的应用程序中时,这对我很有帮助。i、 e.我在
应用程序a
中定义了基本模型,在
应用程序b
中定义了代理模型-django dodn不会自动为我创建内容类型和预任务。