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

查看:263
本文介绍了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中的所有其他对象一样.它们被保存到数据库中,并通过ManyToMany关系链接到用户和组.当您仅在Article模型的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,这意味着,如果您只是删除较旧的权限对象(编辑"),则会对与它有关系的任何东西.如果您不想丢失数据,建议您编写一个脚本以合并两个权限对象,以避免发生任何错误

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天全站免登陆