Search code examples
djangoguardiandjango-rules

Django-rules replacement of guardian.mixins.PermissionListMixin


In my django based application I want to enable users to keep track of their locations. Each location has an owner, and the list view should only show the locations the current user owns.

With django-guardian I was able to achieve the same with specifying the following in my views.py:

from django.views import generic
from guardian.mixins import PermissionRequiredMixin, PermissionListMixin

# Create your views here.
from .models import Location


class LocationListView(PermissionListMixin, generic.ListView):
    model = Location
    permission_required = 'view_location'
    paginate_by = 20
    ordering = ['name']

How would I create something similar with django-rules?


Solution

  • You need to share Location model so we can advise you properly, in both cases you need to specify queryset either in listview variable or by override get queryset method..

    I would suggest you following lines of code assuming location model has foreign key from Auth User model where each location assigned to its owner..

      def get queryset(self):
        qs = Location.objects.filter(user=self.request.user)
         return qs