Adding new custom permissions in Django

前端 未结 5 1543
攒了一身酷
攒了一身酷 2021-01-29 22:46

I am using custom permissions in my Django models like this:

class T21Turma(models.Model):
    class Meta:
        permissions = ((\"can_view_boletim\", \"Can vi         


        
5条回答
  •  耶瑟儿~
    2021-01-29 23:15

    When i runnning migration with following code

    ct, created = orm['contenttypes.ContentType'].objects.get_or_create(model='mymodel',     app_label='myapp') # model must bei lowercase!
    perm, created = orm['auth.permission'].objects.get_or_create(content_type=ct, codename='mymodel_foo')
    

    I getting folloving error

    File "C:\Python26\lib\site-packages\south-0.7.3-py2.6.egg\south\orm.py", line 170, in  __getitem__
    raise KeyError("The model '%s' from the app '%s' is not available in this migration." % (model, app))
    KeyError: "The model 'contenttype' from the app 'contenttypes' is not available in this migration."
    

    To prevent this error, i modified the code

    from django.contrib.contenttypes.models import ContentType
    from django.contrib.auth.models import Permission
    
    class Migration(DataMigration):
    
        def forwards(self, orm):
            "Write your forwards methods here."
            ct = ContentType.objects.get(model='mymodel', app_label='myapp') 
            perm, created = Permission.objects.get_or_create(content_type=ct, codename='mymodel_foo')
            if created:
                perm.name=u'my permission description'
                perm.save()
    

提交回复
热议问题