Django - 清理权限表 [英] Django - Clean permission table

查看:21
本文介绍了Django - 清理权限表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

During development apps and models permissions are sometimes removed or renamed. What's a good way to clean the leftovers from the permissions table without breaking something?

例如:我有一个应用 articles,模型 Article 有一些权限.

For example: I have an app articles with model Article with some permissions.

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):

I add this permission by command (with installed django_extension):

./manage update_permissions

但后来我意识到,最好将其命名为 can_update_title.所以我改变了模型:

But later I realise, that it would be better to name it can_update_title. So I change the model:

class Article(models.Model):
    ...

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

当我更新权限时,Django 管理中有两个权限,这对用户 - 管理员来说真的很困惑.

When I update permissions there are both permissions in Django administration and it is really confusing for users - administrators.

推荐答案

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

Short answer: register the Permission with the admin site. add this to your admin.py file:

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

然后你从那里控制一切.

Then you control everything from there.

长答案: 权限是对象,就像 django 中的所有其他内容一样.它们保存到数据库,并通过多对多关系链接到用户和组.当你在文章模型的 Meta 类中简单地更改权限的名称时,django 无法知道你仍然和以前一样,所以它创建了一个新的对象.

Long answer: Permission are objects, just like everything else in django. They are saved into the database, and linked linked to users and groups through a ManyToMany relationship. When you simply changed the name of the permission in the Meta class of the Article model, django had no way of knowing that you still ment the same object as before, so instead it created a new one.

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

What you should've done was, after changing the name of the permission in the model was to also change the name of the correlating permission object in your database.

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

The easiest way to do this is to register the Permission object with the admin site, so you'd have control from there. You'd still need to change it in both places (models.py and your DB) for any name change, but the admin site makes this easier.

请记住,您创建的额外权限对象(更新")具有新的 pk,这意味着如果您只是删除旧的权限对象(编辑"),则会对任何与它有关的东西.如果您有不想丢失的数据,我建议编写一个脚本来合并这两个 Permission 对象以避免任何错误

Keep in mind that the extra Permission object you created ('update') has a new pk, meaning that if you just delete your older Permission object ('edit') it would have consequences on anything it had a relation with. If you have data that you don't want to lose I suggest writing a script to merge the two Permission objects to avoid any errors

这篇关于Django - 清理权限表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆