Comments (5)
Hi @ciarancourtney,
OK, I see your problem.
You could try to trigger the signal manually like this:
# -*- coding: utf-8 -*-
"""Add permissions for proxy model.
This is needed because of the bug https://code.djangoproject.com/ticket/11154
in Django (as of 1.6, it's not fixed).
When a permission is created for a proxy model, it actually creates if for it's
base model app_label (eg: for "article" instead of "about", for the About proxy
model).
What we need, however, is that the permission be created for the proxy model
itself, in order to have the proper entries displayed in the admin.
"""
import sys
from django.contrib.auth.management import _get_all_permissions
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
from django.apps import apps
from admin_view_permission.apps import update_permissions
class Command(BaseCommand):
help = "Fix permissions for proxy models."
def handle(self, *args, **options):
update_permissions(
apps.get_app_config('app'), # where app is the app of the management command
apps.get_app_config('app'),
verbosity=1,
interactive=True,
using='default',
)
for model in apps.get_models():
opts = model._meta
ctype, created = ContentType.objects.get_or_create(
app_label=opts.app_label,
model=opts.object_name.lower())
for codename, name in _get_all_permissions(opts):
p, created = Permission.objects.get_or_create(
codename=codename,
content_type=ctype,
defaults={'name': name})
if created:
sys.stdout.write('Adding permission {}\n'.format(p))
Although, the admin_view_permission
seems not to work correctly with the proxy models (doesn't return any field to the view). You could try it too. I will have a better look on this tomorrow.
I hope this could help you.
Thanks,
Lefteris
from django-admin-view-permission.
Thanks, I got around the issue by manually creating the admin-view-permission in my proxy meta class.
permissions = (
('view_myuser', "Can view My User"),
)
Then I added a RunPython Data Migration to the autogenerated migration script:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-05-31 16:17
from __future__ import unicode_literals
from django.db import migrations
from django.contrib.auth.management import _get_all_permissions
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
# forward func
def add_myuser_proxy_permissions(apps, schema_editor):
app_config = apps.get_app_config('profiles')
model = app_config.get_model('MyUser')
opts = model._meta
ctype, created = ContentType.objects.get_or_create(
app_label=opts.app_label,
model=opts.object_name.lower(),
)
for codename, name in _get_all_permissions(opts):
p, created = Permission.objects.get_or_create(
codename=codename,
content_type=ctype,
defaults={'name': f'{name} (Proxy)'})
if created:
print(f'Adding permission: {p}')
# reverse func
def delete_myuser_proxy_permissions(apps, schema_editor):
permissions = Permission.objects.filter(codename__icontains='myuser')
permissions.delete()
class Migration(migrations.Migration):
dependencies = [
('profiles', '0015_auto_20170531_0922'),
]
operations = [
migrations.AlterModelOptions(
name='myuser',
options={'permissions': (('view_myuser', 'Can view My User'),), 'verbose_name': 'My User', 'verbose_name_plural': 'My Users'},
),
migrations.RunPython(add_myuser_proxy_permissions, delete_myuser_proxy_permissions),
]
When a user with view_myuser (proxy)
and view_profile
visits the change page of a MyUser, the Django User fields are editable, but this is probably a quirk of the builtin UserAdmin form, The Save buttons do not appear, as expected
from django-admin-view-permission.
Hi @ciarancourtney,
Which Django version do you use? I am testing the behaviour on Django 1.11 and it doesn't return any fields because the code tries to get the local fields and the proxy model doesn't contain any local fields (I will push a fix to master shortly).
Thanks,
Lefteris
from django-admin-view-permission.
I'm on the latest, Django 1.11.2 and DAVP 0.9. Perhaps its because I'm subclassing the UserCreationForm and UserChangeForm
from django-admin-view-permission.
Hi @ciarancourtney,
I am going to close this ticket. I've added the management command(which fixes the permission for the proxy models) to the package and also I've fixed the get_readonly_fields
to return the appropriate fields.
Thanks,
Lefteris
from django-admin-view-permission.
Related Issues (20)
- Change 'Change' button text to 'View' on admin index page if user only has view perm for model HOT 2
- No logs for View permissions HOT 1
- Determine if current admin form is view-only in __init__ HOT 3
- Auth User password hash is shown in default UserAdmin HOT 1
- users and groups admin site HOT 1
- You don't have permission to edit anything at the admin page HOT 9
- User can modify model when adding submit button HOT 2
- Django 2.0 support HOT 6
- Deleting object using Action menu raises KeyError: 'cl' HOT 2
- Users with view permission cannot see custom action HOT 1
- Adding users via /admin/auth/user/add/ throws error because password field doesn't exist HOT 3
- KeyError in admin using add user HOT 1
- Do not work with many to many inlines
- Apply permissions to Customized AdminSite classes HOT 2
- KeyError at `admin details page url` u"Key 'slug' not found in 'ModelForm'. Choices are: ."
- Django 2.1 Error: django.db.utils.IntegrityError: UNIQUE constraint failed: auth_permission.content_type_id, auth_permission.codename HOT 3
- Users with "change" permissions see "delete selected" action in drop down HOT 2
- Management command missing
- request not present in args but in kwargs HOT 1
- Readme not quite accurate HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-admin-view-permission.