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 · 2 comments

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):

@bsiebens
Copy link

bsiebens commented Aug 9, 2024

Depending on your use case, I just handled it in the following way (example):

@rules.predicate
def is_author(user, item):
  if item is not None:
    return user == item.author

  return False

This will allow the rule to pass with a sensible outcome (no item means the user is not the author) and allows the list to be displayed in the admin.

Maybe this needs to be explained in the documentation as a case that can occur and would need to be handled in the rule itself? Also to be noted that the <app_name> permission cannot be set on the model itself but needs to be set using rules.add_perm('<app_name>', rules.always_allow)?

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