私は Python が初めてで、Django 1.3 のクラスベースの汎用ビューを理解しようとしています。現在、カテゴリ内の Location オブジェクトのリストを取得する次のビューがあります。
class category_detail(ListView):
"""Return a generic view of locations in a category."""
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context.
context = super(category_detail, self).get_context_data(**kwargs)
# Add the current category to the context.
category = get_object_or_404(Category, slug=self.kwargs['slug'])
context['category'] = category
return context
def get_queryset(self):
category = get_object_or_404(Category, slug=self.kwargs['slug'])
return Location.objects.filter(category=category)
それは私がやりたいことをします。category
しかし、 2 回定義することで繰り返していることがわかります。category
上部で一度定義したクラスに新しいプロパティを追加して、 and で参照self.category
する方法はget_queryset()
ありget_context_data()
ますか?