Accesing multiple objects with ListView


I have three class in my model “Company” ,“ApprovedList” and “UnapprovedList”.
I’ve created two companies comp_1 comp_2. and created bunch of rows in ApprovedList and UnapprovedList. I want to get list of items by filtering regarding company with ListView.
Is it possible ? If it is, how ?

To make it clear.Here is the code that works for FBV.

def service_list_get(request,pk):
    reg_no = company.reg_no
    context = {
    template = 'company/approved_list.html'
    return render(request,template,context)

(Vitor Freitas) #2

You can create something like

class ServiceListView(ListView):
    model = ApprovedList

    def get_queryset(self):
        queryset = super().get_queryset()  # it's going to retrieve the ListView queryset which is 'ApprovedList'
        company_id = self.request.kwargs.get('company_id')
        company = Company.objects.get(id=company_id)
        reg_no = company.reg_no
        return queryset.filter(reg_no_id=reg_no)

Then your url would need to be something like that:

path('<int:company_id>/services/', ServiceListView.as_view(), name='service_list')