2

I'm working with django 2.0 app and going to update django version to 3.0. But in my project there are few custom permissions named like view_modelname.

class MyConcreteModel(models.Model):
    model_field_1 = models.CharField(max_length=100, blank=False)
    # other fields
    class Meta:
        permissions = (
            ("view_myconcretemodel", "Can see available device interfaces"),
        )      

In django 3 (since v 2.1) such kind of permissions are default. So I got conflict with permission names.

Now I'm trying to rename custom permissions before updating django version.

class MyConcreteModel(models.Model):
    model_field_1 = models.CharField(max_length=100, blank=False)
    # other fields
    class Meta:
        permissions = (
            ("user_view_myconcretemodel", "User can see available device interfaces"),
        )      

After migration 'new' (with new names) permissions were created in DB. But old permissions still there and all users have old permissions (with old names like view_myconcretemodel). Obviously I need 'new' permissions for all users.

Is there possibility simple to rename permissions or give 'new' permissions to relevant users (according to 'old' permissions), and do it automatically?

Torogavr
  • 21
  • 3

1 Answers1

2

After reading how to Programmatically creating permissions https://docs.djangoproject.com/en/3.2/topics/auth/default/#programmatically-creating-permissions

My solution is to modify codename in Permission model. i.e.

from django.contrib.auth.models import Permission

perm = Permission.objects.get(codename="view_myconcretemodel") #old name
perm.codename = "user_view_myconcretemodel" #new name
perm.save()
hsy9045
  • 61
  • 4
  • I did this in a migration: `def update_permission_name(apps, schema_editor): Permission = apps.get_model("auth", "Permission") perm = Permission.objects.get(codename="oldname",content_type_id=ID) perm.codename = "newname" # or you can also set a new name here perm.save()` Worked like a charm ;) – chakmear Jul 07 '22 at 06:13