Adding new custom permissions in Django

前端 未结 5 1532
攒了一身酷
攒了一身酷 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:35

    If you want "manage.py migrate" to do everything (without calling syncdb --all). You need to create new permissions with a migration:

    user@host> manage.py datamigration myapp add_perm_foo --freeze=contenttypes --freeze=auth
    

    Edit the created file:

    class Migration(DataMigration):
    
        def forwards(self, orm):
            "Write your forwards methods here."
            ct, created = orm['contenttypes.ContentType'].objects.get_or_create(
                model='mymodel', app_label='myapp') # model must be lowercase!
            perm, created = orm['auth.permission'].objects.get_or_create(
                content_type=ct, codename='mymodel_foo', defaults=dict(name=u'Verbose Name'))
    

提交回复
热议问题