この質問は少し前に閉じられたことに気付きましたが、他の人に役立つかもしれない場合に備えて、私にとって何がうまくいったかを共有しています.
私が作成したプロキシ モデルのアクセス許可は、( @chirinoskyのように) 親アプリの下にリストされていましたが、非スーパー ユーザーにすべてのアクセス許可を付与したにもかかわらず、プロキシ モデルへのアクセスは依然として拒否されていました。管理者。
私がしなければならなかったのは、既知の 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
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)