2016-08-13 13 views
0

Djangoの汎用ListViewクラスに基づいて作業テーブルを作成しました。しかし、私はソート機能が不足していたので、django-tables2を使って新しいテーブルを生成しました。セットアップはとても簡単でしたが、私の古いListViewクラスから2つの重要な機能を実装する方法はわかりません。 1)ページはログインしているユーザーにのみ表示され、2)ユーザーに基づくフィルタオブジェクトが表示されます。ここでLoginRequiredMixinとdjango-tables2を使ったget_queryset

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) 

views.pyの新しいジャンゴ・tables2機能である:

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

と新しい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',) 

私はLoginRequiredMixinを実装しています(リストページはdjango-tables2を使って私の古いget_queryset(ユーザが見るべき車だけを見ているようにする)?誰も助けることができます。ご協力いただきありがとうございます!

答えて

1

ページを表示する権限を管理するために、機能ベースのビューにはまだ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}) 

CarTableを初期化するときは、適切なフィルタ付きクエリセットを配置する必要があります。

関連する問題