0

I'm trying to create group and add some permissions to it. This is what I did:

students = Group(name='Students')
students.save()   

somemodel_ct = ContentType.objects.get(app_label='course', model='course')

can_view = Permission(name='Can View', codename='can_view_something',
                       content_type=somemodel_ct)
can_view.save()

can_modify = Permission(name='Can Modify', codename='can_modify_something',
                       content_type=somemodel_ct)

can_create = Permission(name='Can Create', codename='can_create_something',
                       content_type=somemodel_ct)

can_delete = Permission(name='Can Delete', codename='can_delete_something',
                       content_type=somemodel_ct)


can_delete.save()

But this seems ugly to me, is there other way (except going to admin page which I would like to avoid)

ekad
  • 14,436
  • 26
  • 44
  • 46
ssapp
  • 313
  • 1
  • 5
  • 13

1 Answers1

0

You can declare custom permissions in your model:

class Entity(models.Model):
    class Meta:
        # Remove default permissions, remove this line to keep them
        default_permissions = dict()

        # Custom permissions 
        permissions = (
            ("add_leadmarket", "Add new lead market"),
            ("edit_leadmarket", "Edit lead market"),
            ("delete_leadmarket", "Delete lead market"),
            ("list_leadmarket", "See list of lead markets"),
            ("view_leadmarket", "View lead market profile"),
        )

    user = models.OneToOneField(User)
    parent = models.ManyToManyField(User, related_name='parents', blank=True)
    # ...
ahmed
  • 5,430
  • 1
  • 20
  • 36