Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django group with permission

from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from api.models import Project
new_group, created = Group.objects.get_or_create(name='new_group')
# Code to add permission to group ???
ct = ContentType.objects.get_for_model(Project)

# Now what - Say I want to add 'Can add project' permission to new_group?
permission = Permission.objects.create(codename='can_add_project',
                                   name='Can add project',
                                   content_type=ct)
new_group.permissions.add(permission)
Comment

creating custom permissions in django

class USCitizen(models.Model):
    # ...
    class Meta:
        permissions = (
            ("can_drive", "Can drive"),
            ("can_vote", "Can vote in elections"),
            ("can_drink", "Can drink alcohol"),
        )
Comment

django group permissions method

group.permissions.set([permission_list])
group.permissions.add(permission, permission, ...)
group.permissions.remove(permission, permission, ...)
group.permissions.clear()
Comment

django programmatically create permissions

from myapp.models import BlogPost
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType

content_type = ContentType.objects.get_for_model(BlogPost)
permission = Permission.objects.create(
    codename='can_publish',
    name='Can Publish Posts',
    content_type=content_type,
)
Comment

PREVIOUS NEXT
Code Example
Python :: arma-garch model python 
Python :: try except to specific line 
Python :: how to use replace in python 
Python :: python matplotlib pyplot set axis equals 
Python :: tkinter auto resize height 
Python :: np random list 
Python :: python create sqlite db file 
Python :: python print without optional argument 
Python :: how to access a file from root folder in python project 
Python :: set comprehension 
Python :: pandas index append value 
Python :: pandas transform count where condition 
Python :: tail a log file with python 
Python :: how to mention a role discord.py 
Python :: aiohttps 
Python :: change xlabel python 
Python :: Maximize Difference codechef solution 
Python :: cascaed models in django 
Python :: new paragraph python 
Python :: python dataframe add rank column 
Python :: python vector class 
Python :: Send Fetch Request Django(Get Method) 
Python :: count items in list python by loop 
Python :: block content 
Python :: python how to make boxplots with jitter 
Python :: functools.cached_property objects in python 
Python :: enormous input test codechef solution 
Python :: change increment in for loop python 
Python :: python using secrets 
Python :: python print set 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =