written by yechoi

[django] shell로 커스텀 유저에 권한 주기 본문

Born 2 Code/Python

[django] shell로 커스텀 유저에 권한 주기

yechoi 2021. 3. 14. 23:14
반응형

쉘 실행

python manage.py shell

 

 

커스텀 유저에 admin 권한 주기

yechoi에게 admin 권한 주기

>>> from django.contrib.auth import get_user_model
>>> User = get_user_model()
>>> yechoi = User.objects.get(login="yechoi")
>>> print(yechoi)
yechoi
>>> yechoi.is_admin
False
>>> yechoi.is_admin = True
>>> yechoi.save()
>>> yechoi.is_admin
True

 

 

커스텀 유저에 커스텀 권한 주기

yechoi에게 can_write_permission 주기

>>> from django.contrib.auth import get_user_model
>>> User = get_user_model()
>>> yechoi = User.objects.get(login="yechoi")
>>> print(yechoi)
yechoi
>>> from django.contrib.contenttypes.models import ContentType
>>> from interview.models import Interview
>>> content_type = ContentType.objects.get_for_model(Interview)
>>> from django.contrib.auth.models import Permission
>>> permission = Permission.objects.get(codename='can_write_interview', content_type=content_type)
>>> yechoi.user_permissions.add(permission)
>>> yechoi.has_perm('interview.can_write_interview')
True
반응형