【问题标题】:Django - Clean permission tableDjango - 清理权限表
【发布时间】:2013-07-25 08:33:43
【问题描述】:

在开发应用程序和模型期间,权限有时会被删除或重命名。有什么好方法可以在不破坏权限的情况下清除权限表中的剩余部分?

例如:我有一个应用程序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 管理中同时存在这两种权限,这让用户(管理员)感到非常困惑。

【问题讨论】:

  • 你的问题有点模糊,介意用一点上下文详细说明吗?
  • 好吧,我写的有点多了。

标签: django permissions


【解决方案1】:

简答:在管理站点注册权限。将此添加到您的 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 对象以避免任何错误

【讨论】:

  • 这有点清楚了。您是否尝试简单地删除旧权限?另外,你为什么需要扩展,在什么方面不够默认?
  • 直接在数据库中删除旧权限很复杂——我们有多台机器,所以我必须编写一些脚本。这当然是解决方案,但我希望有一个简单的解决方案。我们使用django_extension 因为(我认为)默认情况下不是自动更新权限。 + 我忘了,我们也用django-guardian 来连接权限和对象。
  • 对于那些不知道如何使用建议的两条线的人:您可以将它们添加到任何 admin.py (如果您有多个应用程序),然后使用浏览器转到 BASE_URL/管理员/授权/权限
【解决方案2】:

存在一个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2019-03-19
    • 2019-10-27
    • 2021-12-09
    相关资源
    最近更新 更多