1

一般的な見方は私たちの生活を楽にするために存在しますが、これらのものがどのように機能するかを理解するために費やされる時間は実際にはそれらを難しくします。たぶん私ですが、自分で簡単にビューを書いて先に進むことができるように、これを修正する方法を長い間考えようとしていましたが、私はそれを学ぶことを主張しました。

カスタムDetailViewクラスを表示したいのですが、コードは次のようにスローします。

'Sculpture' object has no attribute 'filter'

from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView
from sculptures.models import Sculpture

class SculptureListView(ListView):
    """docstring for SculptureListView"""
    def get_queryset(self):
        return Sculpture.objects.all() 
class SculptureDetailView(DetailView):
    """docstring for SculptureDetailView"""
    def get_queryset(self):
        sculpture = get_object_or_404(Sculpture, slug=self.kwargs['slug'])
        return sculpture

私はそれが1行の修正を必要とすることを知っています-せいぜい、しかし理解できませんでした。

そしてアイデア?

4

2 に答える 2

8

get_queryset、名前が示すように、単一のオブジェクトではなく、クエリセットを返す必要があります。

于 2011-08-24T20:54:44.003 に答える
5

単一のオブジェクトを返すには、get_objectを使用します

class SculptureDetailView(DetailView):
    """docstring for SculptureDetailView"""
    def get_object(self):
        sculpture = get_object_or_404(Sculpture, slug=self.kwargs['slug'])
        return sculpture
于 2012-11-16T03:51:36.333 に答える