Created
February 13, 2021 19:44
-
-
Save eshaan7/ea48ecb41063eaa7f9b98554362df745 to your computer and use it in GitHub Desktop.
adding permission to group in migration [Django]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generated on 2021-02-13 | |
from django.db import migrations | |
def _get_perm_obj(perm: str, apps=None): | |
Permission = apps.get_model("auth", "Permission") | |
app_label, codename = perm.split(".", 1) | |
perm_obj, _created = Permission.objects.get_or_create(content_type__app_label=app_label, | |
codename=codename) | |
return perm_obj | |
def alter_perm_to_allow_job_deletion(apps, schema_editor): | |
# We can't import the model directly as it may be a newer | |
# version than this migration expects. We use the historical version. | |
Group = apps.get_model("auth", "Group") | |
grp, _grp_created = Group.objects.get_or_create(name="DefaultGlobal") | |
perm_obj = _get_perm_obj("api_app.delete_job", apps=apps) | |
grp.permissions.add(perm_obj) | |
class Migration(migrations.Migration): | |
dependencies = [ | |
("api_app", "0004_auto_20201112_0021"), | |
# added dependency to enable using models from 3rd party app in our app | |
("guardian", "0001_initial"), | |
] | |
operations = [ | |
migrations.RunPython(alter_perm_to_allow_job_deletion), | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment