Django: DetailView implementing a get_queryset()
Asked Answered
G

2

6

I'm getting this following error:

ImproperlyConfigured at /elearning/7447932a-6044-498a-b902-97cbdd0a4001/
DetailView is missing a QuerySet. Define DetailView.model, DetailView.queryset, or override DetailView.get_queryset().

Following the Django documentation on DetailView the get_query is not mandatory unless that I want to override it.

view.py

class CourseDetailView(DetailView):

    model = Course
    template_name='elearning/detail.html'

    def get_object(self):
        course = get_object_or_404(Course, pk=self.kwargs['pk'])
        return self.model.objects.filter(pk=pk)

    def get_context_data(self, **kwargs):
        context = super(CourseDetailView, self).get_context_data(**kwargs)
        context['now'] = timezone.now()
        return context

urls.py

url(r'^(?P<pk>[0-9a-z-]+)/$', views.DetailView.as_view(), name='course-detail'),

listview template

 <a href="{% url 'elearning:course-detail' article.course_id %}">{{ article.title }}</a>

models.py

class Course(models.Model):
    course_id = models.UUIDField(default=uuid.uuid4, editable=False)
    ...

I would like to know why should I implement a get_queryset()?

I still get the same error when I add a get_queryset()

def get_queryset(self):
    qs = super(CourseDetailView, self).get_queryset()
    return qs.filter(pk=self.kwargs['pk'])
Glantz answered 25/7, 2017 at 6:44 Comment(2)
Can we see your DetailView class?Kathyrnkati
@Kathyrnkati you right DetailView should be named ` CourseDetailView` on urls.py .. you can posted the answerGlantz
S
7

your view is named CourseDetailView but you are using DetailView in url

url(r'^(?P<pk>[0-9a-z-]+)/$', views.DetailView.as_view(), name='course-detail'),

so the url will be

url(r'^(?P<pk>[0-9a-z-]+)/$', views.CourseDetailView.as_view(), name='course-detail'),
Schaerbeek answered 25/7, 2017 at 6:56 Comment(0)
M
3

It may be worth adding queryset = Course.objects.all() to your view to be a bit more verbose and solve the error.

As for def get_queryset(self), you may want to use this to perform some custom filtering on your QuerySet. I'm going to provide an example that shows how you might use the def get_queryset(self) method to return only the pages from a single book. I've included multiple url patterns for completeness, but only the relevant view class that implements get_queryset(self)

# models.py
class Book(models.Model):
    title = models.CharField(max_length=32)

class Page(models.Model):
    book = models.ForeignKey(Book)
    page_num = models.IntegerField()

# views.py
class PageDetailView(DetailView):
    queryset = Page.objects.all()

    def get_queryset(self):
        """Filter pages by a book"""
        return self.queryset.filter(book_id=self.kwargs.get('book_id'))

# urls.py
urlpatterns = [
    url(
        r'^books/$',
        views.BookListView.as_view(),
        name='book-list',
    ),
    url(
        r'^books/(?P<pk>\d+)/$',
        views.BookDetailView.as_view(),
        name='book-detail',
    ),
    url(
        r'^books/(?P<book_id>\d+)/pages/$',
        views.PageListView.as_view(),
        name='page-list',
    ),
    url(
        r'^books/(?P<book_id>\d+)/pages/(?P<pk>\d+)/$',
        views.PageDetailView.as_view(),
        name='page-detail',
    ),
]
Morgenthaler answered 25/7, 2017 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.