[Fixed]-LoginRequiredMixin and get_queryset with django-tables2

1👍

For managing permission to view page you can still use login_required decorator for your function-based views.

from django.contrib.auth.decorators import login_required    


@login_required
def car(request):
    table = CarTable(Car.objects.all())
    RequestConfig(request, paginate={'per_page': 25}).configure(table)
    return render(request, 'car.html', {'table': table})

And you should put correct filtered queryset when initializing CarTable.

0👍

For filtering objects, you can pass the filtered QuerySet to the table rather than all.

def car(request):
    cars = Car.objects.all()
    # filter the objects
    if not request.user.is_staff:
        cars = cars.filter(bureau=request.user.bureau, active = 1)
    table = CarTable(cars)
    RequestConfig(request, paginate={'per_page': 25}).configure(table)
    return render(request, 'car.html', {'table': table})

Leave a comment