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(ユーザが見るべき車だけを見ているようにする)?誰も助けることができます。ご協力いただきありがとうございます!