在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):django-guardian/django-guardian开源软件地址(OpenSource Url):https://github.com/django-guardian/django-guardian开源编程语言(OpenSource Language):Python 92.1%开源软件介绍(OpenSource Introduction):django-guardian
DocumentationOnline documentation is available at https://django-guardian.readthedocs.io/. Requirements
GitHub Actions run tests against Django versions 2.2, 3.0, 3.1, 3.2, 4.0, and main. InstallationTo install pip install django-guardian ConfigurationWe need to hook
INSTALLED_APPS = (
...
'guardian',
)
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # default
'guardian.backends.ObjectPermissionBackend',
)
UsageAfter installation and project hooks we can finally use object permissions with Django. Lets start really quickly: >>> from django.contrib.auth.models import User, Group
>>> jack = User.objects.create_user('jack', '[email protected]', 'topsecretagentjack')
>>> admins = Group.objects.create(name='admins')
>>> jack.has_perm('change_group', admins)
False
>>> from guardian.models import UserObjectPermission
>>> UserObjectPermission.objects.assign_perm('change_group', jack, obj=admins)
<UserObjectPermission: admins | jack | change_group>
>>> jack.has_perm('change_group', admins)
True Of course our agent jack here would not be able to change_group globally: >>> jack.has_perm('change_group')
False Admin integrationReplace For example: from django.contrib import admin
from myapp.models import Author
from guardian.admin import GuardedModelAdmin
# Old way:
#class AuthorAdmin(admin.ModelAdmin):
# pass
# With object permissions support
class AuthorAdmin(GuardedModelAdmin):
pass
admin.site.register(Author, AuthorAdmin)
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论