Django - Clean permission table

2019-02-16 21:14发布

问题:

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?

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'),
        )

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

./manage update_permissions

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'),
        )

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

回答1:

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.

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.

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.

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