django-permissions

How to add a permission to a user/group during a django migration?

一笑奈何 提交于 2019-12-04 22:13:16
问题 I would like to execute the following migration: # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.contrib.auth.models import Permission from django.db import migrations from django.conf import settings from django.contrib.auth.models import Group, User def add_api_group(apps, schema_editor): Group.objects.create(name=settings.API_USER_GROUP) # get_or_create returns a tuple, not a Group group = Group.objects.get(name=settings.API_USER_GROUP) permissions = Permission

How to add custom permission to the User model in django?

偶尔善良 提交于 2019-12-04 15:54:09
问题 in django by default when syncdb is run with django.contrib.auth installed, it creates default permissions on each model... like foo.can_change , foo.can_delete and foo.can_add. To add custom permissions to models one can add class Meta: under the model and define permissions there, as explained here https://docs.djangoproject.com/en/dev/topics/auth/#custom-permissions My question is that what should I do if I want to add a custom permission to the User model? like foo.can_view. I could do

can't change user permissions during unittest in django

这一生的挚爱 提交于 2019-12-04 09:03:11
I've finally decided to make some tests for my apps but I'm stuck on testing if a user can change another user (depends on the type of the user -- I use django-rules to be able to do logical permission checks, but this is not important) Here's the code I have so far class RulesAndPermissionsTests(TestCase): fixtures = ['auth_no_permissions.json', 'profiles.json', 'rules.json'] def setUp(self): self.c = Client() self.user = User.objects.get(username="estagiario") self.non_staff = User.objects.get(username="fisica") self.admin = User.objects.get(username="admin") login = self.c.login(username=

Django admin - change permissions list

余生长醉 提交于 2019-12-04 08:40:44
问题 Is there any possibility to change permissions list in user edit page? I don't wan't to show all of permissions for example admin log entry or auth group etc. How can I modify a main queryset to exclude some of it? 回答1: I got the idea from this topic, which also answer your question, but it's not that clear. You have to overwrite the queryset of user permissions in the UserAdmin form used for visualization. To do this, the easiest way is to create a subclass of UserAdmin and overwrite the get

How do I define default permissions for users in Django Guardian?

只愿长相守 提交于 2019-12-04 03:33:01
问题 When I create a user in Django, he has no permissions: In [7]: u = User.objects.create(username='aoeu') In [12]: u.user_permissions.all() Out[12]: [] I want some permissions to be set by default (say, 'api.add_item'), and I use Django Guardian. Is this possible to do in a declarative way, eg. without writing a post_save signal? 回答1: No, it is not possible. Check django.contrib.auth code to ensure 来源: https://stackoverflow.com/questions/12312931/how-do-i-define-default-permissions-for-users-in

How to add a permission to a user/group during a django migration?

浪子不回头ぞ 提交于 2019-12-03 14:11:09
I would like to execute the following migration: # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.contrib.auth.models import Permission from django.db import migrations from django.conf import settings from django.contrib.auth.models import Group, User def add_api_group(apps, schema_editor): Group.objects.create(name=settings.API_USER_GROUP) # get_or_create returns a tuple, not a Group group = Group.objects.get(name=settings.API_USER_GROUP) permissions = Permission.objects.filter(codename__in = [ 'add_topic', ]) group.permissions.add(*permissions) def add_api_user

How to add custom permission to the User model in django?

我们两清 提交于 2019-12-03 09:57:36
in django by default when syncdb is run with django.contrib.auth installed, it creates default permissions on each model... like foo.can_change , foo.can_delete and foo.can_add. To add custom permissions to models one can add class Meta: under the model and define permissions there, as explained here https://docs.djangoproject.com/en/dev/topics/auth/#custom-permissions My question is that what should I do if I want to add a custom permission to the User model? like foo.can_view. I could do this with the following snippet, ct = ContentType.objects.get(app_label='auth', model='user') perm =

row level permissions in django

て烟熏妆下的殇ゞ 提交于 2019-12-03 02:26:11
问题 Is there a way to do row level permissions in django? I thought there wasn't but just noticed this in the docs: Permissions can be set not only per type of object, but also per specific object instance. By using the has_add_permission(), has_change_permission() and has_delete_permission() methods provided by the ModelAdmin class, it is possible to customize permissions for different object instances of the same type. https://docs.djangoproject.com/en/dev/topics/auth/ But i don't see any

Django admin - change permissions list

空扰寡人 提交于 2019-12-03 00:30:38
Is there any possibility to change permissions list in user edit page? I don't wan't to show all of permissions for example admin log entry or auth group etc. How can I modify a main queryset to exclude some of it? I got the idea from this topic , which also answer your question, but it's not that clear. You have to overwrite the queryset of user permissions in the UserAdmin form used for visualization. To do this, the easiest way is to create a subclass of UserAdmin and overwrite the get_form method: from django.contrib.auth.models import User from django.contrib.auth.admin import UserAdmin

How can I use Django permissions without defining a content type or model?

二次信任 提交于 2019-12-03 00:14:13
问题 I'd like to use a permissions based system to restrict certain actions within my Django application. These actions need not be related to a particular model (e.g. access to sections in the application, searching...), so I can't use the stock permissions framework directly, because the Permission model requires a reference to an installed content type. I could write my own permission model but then I'd have to rewrite all the goodies included with the Django permissions, such as: The