简体   繁体   English

LoginRequiredMixin 和 get_queryset 与 django-tables2

[英]LoginRequiredMixin and get_queryset with django-tables2

I already had a working table built on Django's generic ListView class.我已经有一个基于 Django 的通用 ListView 类构建的工作表。 But I was missing the ability to sort and I therefore took a look at generating a new table using django-tables2.但是我缺少排序的能力,因此我研究了使用 django-tables2 生成一个新表。 It was very easy to set up, but I don't know how to implement two important features from my old ListView class;设置起来非常容易,但我不知道如何从我的旧 ListView 类中实现两个重要功能; 1) page is only visible to logged in users and 2) filter objects based on user. 1) 页面仅对登录用户可见,2) 基于用户过滤对象。

Here is my old class in views.py :这是我在views.py旧课程:

class CarList(LoginRequiredMixin, ListView):
    model = Car
    paginate_by = 20

def get_queryset(self):
    qs = super().get_queryset()
    return qs if self.request.user.is_staff else qs.filter(bureau=self.request.user.bureau, active = 1)

Here is the new django-tables2 function in views.py :这是views.py的新 django-tables2 函数:

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

and the new tables.py :和新的tables.py

import django_tables2 as tables
from app.models import Feriehus
from django.contrib.auth.mixins import LoginRequiredMixin

class CarTable(tables.Table):
    class Meta:
        model = Car
        attrs = {'class': 'paleblue'}
        fields = ('car_id', 'type', 'price', 'year',)

How do I implement LoginRequiredMixin (so that the list page is only visible to logged in users) and my old get_queryset (so that user only see the cars they are supposed to see) with django-tables2?我如何使用 django-tables2 实现 LoginRequiredMixin(以便列表页面仅对登录用户可见)和我的旧 get_queryset(以便用户只能看到他们应该看到的汽车)? Can anyone help.任何人都可以帮忙。 Thank you very much for any assistance!非常感谢您的任何帮助!

For managing permission to view page you can still use login_required decorator for your function-based views.为了管理查看页面的权限,您仍然可以为基于函数的视图使用login_required装饰器。

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.并且您应该在初始化 CarTable 时放置正确的过滤查询集。

For filtering objects, you can pass the filtered QuerySet to the table rather than all .对于过滤对象,您可以将过滤后的 QuerySet 传递给表而不是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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM