Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin_url with view permission #518

Open
covadonga99 opened this issue Nov 30, 2022 · 1 comment
Open

admin_url with view permission #518

covadonga99 opened this issue Nov 30, 2022 · 1 comment

Comments

@covadonga99
Copy link

Hello!
Why you need change permissions to click in the changelist view of a model? I have a model user in the core app and I have a group with add and view permissions to this model but not change permissions and this group can't access to this view from the side menu but it can access to the changelist view if it clicks in the core app from the side menu it can click on the view of the users (or entering with the url). This is because of this piece of code:
```
if perms.get('change', False):
try:
model_dict['admin_url'] = reverse('admin:%s_%s_changelist' % info, current_app=admin_site.name)
except NoReverseMatch:
pass

Doesn't this perms.get("change", False) should be perms.get("view", False)?
@ljluestc
Copy link

You can create a subclass of ModelAdmin and override the has_change_permission method to check for the 'view' permission instead. Here's an example:

from django.contrib import admin

class CustomModelAdmin(admin.ModelAdmin):
def has_change_permission(self, request, obj=None):
opts = self.opts
codename = get_permission_codename('view', opts)
return request.user.has_perm('%s.%s' % (opts.app_label, codename))

Then, register your model using the CustomModelAdmin instead of the default admin.ModelAdmin.

from django.contrib import admin
from myapp.models import MyModel

admin.site.register(MyModel, CustomModelAdmin)
With this customization, users who have the 'view' permission for the model will be able to access the change list view in the admin side menu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants