Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove old permissions in django

Tags:

django

In my Django site there are some permissions entries linked to applications that I've removed. For example I have permissions entries linked to "Dashboard" and "Jet" applications. How can you remove them?

like image 547
Mark116 Avatar asked Nov 08 '17 14:11

Mark116


1 Answers

To start, make an empty migration file:

python manage.py makemigrations --empty yourappname

Change the migration (this is an example, adjust to your needs):

# -*- coding: utf-8 -*-
from __future__ import unicode_literals    
from django.db import migrations    


def add_permissions(apps, schema_editor):
    pass


def remove_permissions(apps, schema_editor):
    """Reverse the above additions of permissions."""
    ContentType = apps.get_model('contenttypes.ContentType')
    Permission = apps.get_model('auth.Permission')
    content_type = ContentType.objects.get(
        model='somemodel',
        app_label='yourappname',
    )
    # This cascades to Group
    Permission.objects.filter(
        content_type=content_type,
        codename__in=('add_somemodel', 'change_somemodel', 'delete_somemodel'),
    ).delete()

class Migration(migrations.Migration):
    dependencies = [
        ('yourappname', '0001_initial'),
    ]
    operations = [
        migrations.RunPython(remove_permissions, add_permissions),
    ]
like image 96
allcaps Avatar answered Sep 20 '22 17:09

allcaps