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

AttributeError: 'NoneType' when trying to access the admin panel view list #185

Open
omargawdat opened this issue Apr 19, 2024 · 1 comment

Comments

@omargawdat
Copy link

I am getting this error when trying to access the view list in the admin panel: AttributeError, as no selected provider is selected when I try to access the providers list!!
'NoneType' object has no attribute 'company'
image

rules:

import rules

@rules.predicate
def is_company_agent(user, provider):
    return provider.company == user


# Permissions setup for Provider model
rules.add_perm('users.change_provider', is_company_agent)
rules.add_perm('users.delete_provider', is_company_agent)
rules.add_perm('users.view_provider', is_company_agent)

admin code:

@admin.register(Provider)
class ProviderAdmin(ObjectPermissionsModelAdmin):
@omargawdat
Copy link
Author

omargawdat commented Apr 19, 2024

UPDATE 1.0:

to solve the issue use this mixin instead of the default one this will solve the error.

class ObjectPermissionsMixin:
    def get_queryset(self, request):
        queryset = super().get_queryset(request)
        if request.user.is_superuser:
            return queryset
        return queryset.filter(pk__in=[item.pk for item in queryset if self.has_view_permission(request, item)])

    def has_permission(self, request, obj, perm_type):
        if not obj:
            return True
        codename = get_permission_codename(perm_type, self.opts)
        permission = f"{self.opts.app_label}.{codename}"
        return request.user.has_perm(permission, obj)

    def has_view_permission(self, request, obj=None):
        return self.has_permission(request, obj, 'view') or self.has_change_permission(request, obj)

    def has_change_permission(self, request, obj=None):
        return self.has_permission(request, obj, 'change')

    def has_delete_permission(self, request, obj=None):
        return self.has_permission(request, obj, 'delete')

@admin.register(Provider)
class ProviderAdmin(ObjectPermissionsMixin, ModelAdmin):

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

1 participant