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'])
get_queryset(self)Returns the queryset that should be used for list views, and that should be used as the base for lookups in detail views. Defaults to returning the queryset specified by the queryset attribute. This method should always be used rather than accessing self. queryset directly, as self.
get_object() looks for a pk_url_kwarg argument in the arguments to the view; if this argument is found, this method performs a primary-key based lookup using that value. If this argument is not found, it looks for a slug_url_kwarg argument, and performs a slug lookup using the slug_field .
In this shot, we are going to learn how to get the details of a particular instance. Detail view refers to a view that displays a particular instance of a table from the database with all the necessary details. Detail view is used to display multiple types of data on a single page or view, e.g., the details of a user.
DetailView should be used when you want to present detail of a single model instance. DetailView shouldn't be used when your page has forms and does creation or update of objects. FormView, CreateView and UpdateView are more suitable for working with forms, creation or updation of objects.
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'),
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',
),
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With