通用视图的存在是为了让我们的生活更轻松,但花在理解这些东西如何工作上的时间实际上让它们变得更加困难。也许是我的问题,但我已经尝试了很长时间来解决这个问题,我可以很容易地自己编写视图,然后继续前进,但我坚持要学习它。
我想要显示一个自定义的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我知道它最多需要一行修复--但我无法弄清楚。
还有想法呢?
发布于 2011-08-25 04:54:44
顾名思义,get_queryset应该返回一个查询集,而不是单个对象。
发布于 2012-11-16 11:51:36
要返回单个对象,请使用get_object
class SculptureDetailView(DetailView):
"""docstring for SculptureDetailView"""
def get_object(self):
sculpture = get_object_or_404(Sculpture, slug=self.kwargs['slug'])
return sculpturehttps://stackoverflow.com/questions/7182080
复制相似问题