Search code examples
djangodjango-viewsdjango-templatesdjango-generic-views

Django dynamic filtered ListView returning error: get() got multiple values for argument 'self'


I need some help trouble shooting this. I think I'm doing what is suggested by the django docs (https://docs.djangoproject.com/en/3.1/topics/class-based-views/generic-display/), but I keep getting this error: get() got multiple values for argument 'self'

textbook_list.html

<a href="{% url 'lesson_list' textbook.pk %}">
    <button type="button" class="btn btn-info">More Info</button>
</a>

urls.py

urlpatterns = [path('grade/<int:pk>/', TextbookLessonList.as_view(), name='lesson_list')]

views.py

class TextbookLessonList(ListView):
    template_name = 'textbook_lesson_list.html' 

    def get_queryset(self):
        self.textbook = get_object_or_404(Textbook, self=self.kwargs['pk']) #This is the offending line
        return TextbookLesson.objects.filter(textbook.pk==self.textbook)

Solution

  • It does not make much sense to use self=…, you want to filter on the primary key, so:

    class TextbookLessonList(ListView):
        template_name = 'textbook_lesson_list.html' 
    
        def get_queryset(self):
            self.textbook = get_object_or_404(Textbook, pk=self.kwargs['pk'])
            return TextbookLesson.objects.filter(textbook=self.textbook)

    You however do not per se need to access the textbook, if you are only interested in the TextbookLessons, you can filter directly with:

    class TextbookLessonList(ListView):
        template_name = 'textbook_lesson_list.html'
        model = TextbookLesson
    
        def get_queryset(self, *args, **kwargs):
            return super().get_queryset(*args, **kwargs).filter(
                textbook_id=self.kwargs['pk']
            )