[Django]-TypeError get_queryset() missing 1 required positional argument: 'pk'

5👍

✅

The pk is not a parameter in the get_queryset, only the self is.

The positional and named parameters of the URL, are stored in self.args and self.kwargs respectively.

So you can fix the get_queryset to:

class group1ListView(LoginRequiredMixin,ListView):
    model = group1

    def get_queryset(self):  # no pk parameter
        company_details = get_object_or_404(company, pk=self.kwargs['pk'])
        return self.model.objects.filter(
            User=self.request.user,
            Company=company_details.id
        )

You can however boost efficiency by writing a single query (but the semantics is slightly different), like:

class group1ListView(LoginRequiredMixin,ListView):
    model = group1
    allow_empty = False

    def get_queryset(self):  # no pk parameter
        return self.model.objects.filter(
            User=self.request.user,
            Company=self.kwargs['pk']
        )

We can add an allow_empty = False in case we want to raise a 404 when no such group1s can be found.

Your urls.py of course needs to pass the pk value, for example:

# app/urls.py

urlpatterns = [
    url(r'^(?P<pk>\d+)$',views.group1ListView.as_view(),name='grouplist')
]

So you then can query the groups of a company with a path that has the id of the company. If this is the root URL, it is thus something like host.org/123

I think it is however advisable to rename the parameter to company_pk, since right now, you “hint” that this is the pk of the group1 model, not of the Company. This confusion can result in queries that look correct, but are not. Most developers will find it strange if they see a query like

group1.objects.get(pk=self.kwargs['company_pk'])

but not when they see

group1.objects.get(pk=self.kwargs['pk'])

Leave a comment