19

在开发应用程序和模型期间,权限有时会被删除或重命名。什么是从权限表中清除剩余而不破坏某些东西的好方法?

例如:我有一个具有某些权限articles的模型的应用程序。Article

class Article(models.Model):
    title = ...
    text = ...

    class Meta:
        permissions = (
            ('can_edit_title', 'Can edit title of article'),
            ('can_edit_text', 'Can edit text of article'),
        )

我通过命令添加此权限(已安装django_extension):

./manage update_permissions

但后来我意识到,命名它会更好can_update_title。所以我改变了模型:

class Article(models.Model):
    ...

    class Meta:
        permissions = (
            ('can_update_title', 'Can update title of article'),
            ('can_update_text', 'Can update text of article'),
        )

当我更新权限时,Django 管理中有两种权限,这让用户 - 管理员感到非常困惑。

4

2 回答 2

41

简短的回答:在管理站点注册权限。将此添加到您的 admin.py 文件中:

from django.contrib.auth.models import Permission
admin.site.register(Permission)

然后你从那里控制一切。

长答案:权限是对象,就像 django 中的其他所有内容一样。它们被保存到数据库中,并通过多对多关系链接到用户和组。当您只是在 Article 模型的 Meta 类中更改权限名称时,django 无法知道您仍然和以前一样使用相同的对象,因此它创建了一个新对象。

您应该做的是,在更改模型中的权限名称之后,还要更改数据库中相关权限对象的名称。

最简单的方法是在管理站点注册 Permission 对象,这样您就可以从那里进行控制。您仍然需要在两个地方(models.py 和您的数据库)更改它以进行任何名称更改,但管理站点使这更容易。

请记住,您创建的额外 Permission 对象('update')有一个新的 pk,这意味着如果您只是删除旧的 Permission 对象('edit'),它会对与之相关的任何内容产生影响。如果您有不想丢失的数据,我建议您编写一个脚本来合并两个 Permission 对象以避免任何错误

于 2013-07-25T14:34:15.047 回答
0

存在一个 Django 内置管理命令,负责删除/删除过时的内容类型 - 在删除过时的内容类型时,此命令还会删除相关权限:

./manage.py remove_stale_contenttypes [--include-stale-apps]

它还接受一个可选参数--include-stale-apps,导致:

“删除过时的内容类型,包括以前安装的应用程序中已从 INSTALLED_APPS 中删除的内容类型。”

引用自命令帮助文本。官方文档:https ://docs.djangoproject.com/en/4.0/ref/django-admin/#remove-stale-contenttypes

此命令让用户确认点击(内容类型和权限),您还可以选择每次都回答“不,不做任何事情”;例子:

./manage.py remove_stale_contenttypes --include-stale-apps
Some content types in your database are stale and can be deleted.
Any objects that depend on these content types will also be deleted.
The content types and dependent objects that would be deleted are:

    - Content type for core.affiliation
    - 4 auth.Permission object(s)
    ...

This list doesn't include any cascade deletions to data outside of Django's
models (uncommon).

Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel: yes
Some content types in your database are stale and can be deleted.
Any objects that depend on these content types will also be deleted.
The content types and dependent objects that would be deleted are:

    - Content type for foo.bar
    - 3 auth.Permission object(s)
    - 3 auth.Group_permissions object(s)
    - Content type for foo.baz
    - 3 auth.Permission object(s)
    - 6 auth.Group_permissions object(s)
    ...

This list doesn't include any cascade deletions to data outside of Django's
models (uncommon).

Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel: yes
Some content types in your database are stale and can be deleted.
Any objects that depend on these content types will also be deleted.
The content types and dependent objects that would be deleted are:

    - Content type for misc.whatever
    - 4 auth.Permission object(s)
    ...

This list doesn't include any cascade deletions to data outside of Django's
models (uncommon).

Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel: yes
于 2022-01-09T13:39:00.030 回答