19

默认情况下,在 django 中,当安装了 django.contrib.auth 运行 syncdb 时,它会在每个模型上创建默认权限......比如 foo.can_change 、 foo.can_delete 和 foo.can_add。要向模型添加自定义权限,可以在模型下添加类 Meta: 并在此处定义权限,如此处所述https://docs.djangoproject.com/en/dev/topics/auth/#custom-permissions

我的问题是,如果我想为 User 模型添加自定义权限该怎么办?像 foo.can_view。我可以用下面的代码片段做到这一点,

ct = ContentType.objects.get(app_label='auth', model='user')
perm = Permission.objects.create(codename='can_view', name='Can View Users', 
                                  content_type=ct)
perm.save()

但我想要一些与syncdb 配合得很好的东西,例如我的自定义模型下的Meta 类。我是否应该将这些放在 Meta: 类中,在 UserProfile 下,因为这是扩展用户模型的方式。但这是正确的方法吗?这不会将它与 UserProfile 模型联系起来吗?

4

4 回答 4

7

你可以这样做:

__init__.py您的 Django 应用程序中添加:

from django.db.models.signals import post_syncdb
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth import models as auth_models
from django.contrib.auth.models import Permission

# custom user related permissions
def add_user_permissions(sender, **kwargs):
    ct = ContentType.objects.get(app_label='auth', model='user')
    perm, created = Permission.objects.get_or_create(codename='can_view', name='Can View Users', content_type=ct)
post_syncdb.connect(add_user_permissions, sender=auth_models)
于 2012-11-13T12:30:09.523 回答
4

我不认为这里有一个“正确”的答案,但是我使用了与您完全相同的代码,除了我更改Permission.objects.createPermission.objects.get_or_create并且可以使用 find 与 syncdb 同步

于 2012-03-09T20:17:20.037 回答
2

Django 1.8 的更新答案。使用信号pre_migrate代替,因为不推荐使用 syncdb 并且如果信号会改变数据库pre_syncdb,文档建议使用pre_migrate代替。post_migrate此外,@receiver用于连接add_user_permissions信号。

from django.db.models.signals import pre_migrate
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth import models as auth_models
from django.contrib.auth.models import Permission
from django.conf import settings
from django.dispatch import receiver


# custom user related permissions
@receiver(pre_migrate, sender=auth_models)
def add_user_permissions(sender, **kwargs):
    content_type = ContentType.objects.get_for_model(settings.AUTH_USER_MODEL)
    Permission.objects.get_or_create(codename='view_user', name='View user', content_type=content_type)
于 2015-08-01T14:42:04.780 回答
0

这有点hacky,但无论如何在这里提到它以供参考。

我的网站有一个名为 的通用模型Setting,它存储有关我希望某些用户能够编辑的网站的各种设置,而无需通过我的开发人员(如注册限制、地址或项目成本等) .

所有不能很好地映射到其他模型的权限(例如“向学生发送密码提醒电子邮件”、“生成付款核对报告”、“生成 PDF 收据”),它们实际上只与在管理区域中查看的页面相关,被倾倒在这个Setting模型上。

例如,这是模型:

class Setting(models.Model):
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(editable=False)
    description = models.TextField()
    value = models.TextField()
    class Meta:
        #for permissions that don't really relate to a particular model, and I don't want to programmatically create them.
        permissions = (
            ("password_reminder", "Send Password Reminder"),
            ("generate_payment_reconciliation_report", "Generate Payment Reconciliation Report"),
            ("generate_pdf_receipt", "Generate PDF Receipt"),
        )

这些设置中的每一个是否都与Setting模型严格相关?不,这就是为什么我说这有点骇人听闻。但是很高兴我现在可以在这里转储所有这些权限,而 Django 的迁移命令将负责其余的工作。

于 2019-06-14T18:22:05.447 回答