Django に新しいカスタム権限を追加する
-
20-09-2019 - |
質問
私は Django モデルで次のようにカスタム権限を使用しています。
class T21Turma(models.Model):
class Meta:
permissions = (("can_view_boletim", "Can view boletim"),
("can_view_mensalidades", "Can view mensalidades"),)
問題は、リストにアクセス許可を追加しても、そのアクセス許可がリストに追加されないことです。 auth_permission
syncdb を実行するときのテーブル。私は何を間違っているのでしょうか。何か違いがある場合は、データベースの移行にSouthを使用しています。
解決
南はdjango.contrib.auth権限を追跡することはありません。詳細については、チケット#211 のを参照してください。
チケットのコメントの一つは、syncdbの実行に--all
オプションを使用しても問題が解決しうることを示唆している。
他のヒント
あなたがしたい場合(syncdbの実行の--allを呼び出すことなく)すべてを行うために、「移行manage.pyの」。あなたは、移行に新しい権限を作成する必要があります:
user@host> manage.py datamigration myapp add_perm_foo --freeze=contenttypes --freeze=auth
作成したファイルを編集します:
class Migration(DataMigration):
def forwards(self, orm):
"Write your forwards methods here."
ct, created = orm['contenttypes.ContentType'].objects.get_or_create(
model='mymodel', app_label='myapp') # model must be lowercase!
perm, created = orm['auth.permission'].objects.get_or_create(
content_type=ct, codename='mymodel_foo', defaults=dict(name=u'Verbose Name'))
に接続できます。 post_migrate
移行後に権限を更新するためにシグナルを送信します。以下のコードを少し変更して使用します。 情熱を持った開発者 そして元々は ジャンゴ拡張機能.
# Add to your project-level __init__.py
from south.signals import post_migrate
def update_permissions_after_migration(app,**kwargs):
"""
Update app permission just after every migration.
This is based on app django_extensions update_permissions management command.
"""
from django.conf import settings
from django.db.models import get_app, get_models
from django.contrib.auth.management import create_permissions
create_permissions(get_app(app), get_models(), 2 if settings.DEBUG else 0)
post_migrate.connect(update_permissions_after_migration)
iが、次のコード
と移動を走っct, created = orm['contenttypes.ContentType'].objects.get_or_create(model='mymodel', app_label='myapp') # model must bei lowercase!
perm, created = orm['auth.permission'].objects.get_or_create(content_type=ct, codename='mymodel_foo')
私は、次の取得中にエラー
File "C:\Python26\lib\site-packages\south-0.7.3-py2.6.egg\south\orm.py", line 170, in __getitem__
raise KeyError("The model '%s' from the app '%s' is not available in this migration." % (model, app))
KeyError: "The model 'contenttype' from the app 'contenttypes' is not available in this migration."
このエラーを防止するために、私は、修正されたコード
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
class Migration(DataMigration):
def forwards(self, orm):
"Write your forwards methods here."
ct = ContentType.objects.get(model='mymodel', app_label='myapp')
perm, created = Permission.objects.get_or_create(content_type=ct, codename='mymodel_foo')
if created:
perm.name=u'my permission description'
perm.save()
所属していません StackOverflow