Sreekanth Reddy Balne
Sreekanth Reddy Balne

Reputation: 3424

Django Generic View - ListView from an instance of DetailView

I am using Generic views DetailView and ListView

I have three models as such User, Business and Invoice. A User can have multiple businesses can have multiple invoices.

#mixins.py
class BusinessOwnerRequiredMixin(object):

    def has_permissions(self):
        obj = self.get_object()
        if isinstance(obj, Business):
        # Assumes that your Article model has a foreign key called `auteur`.
            return obj.owner == self.request.user

    def dispatch(self, request, *args, **kwargs):
        if not self.has_permissions():
            raise PermissionDenied
        return super(BusinessOwnerRequiredMixin, self).dispatch(request, *args, **kwargs)

#views.py
class BusinessDashboard(BusinessOwnerRequiredMixin, DetailView):
    model = Business
    template_name = "business/business-main.html"

class InvoiceListView(BusinessDashboard):
    template_name = "business/purchase/purchase_invoice-main.html"

class InvoiceDetailView(InvoiceListView):
    template_name = "business/purchase/purchase_invoice.html"

#urls.py
path(r'business/<pk>/purchase_invoices/<pid>/',vw.PurchaseInvoiceDetailView.as_view(), name='purchase_invoice'),
path(r'business/<pk>/purchase_invoices/',vw.PurchaseInvoiceListView.as_view(), name='purchase_invoices')

What I am looking for is to inherit the ListView of Invoice from DetailView of Business i.e, from an instance of a Business i.e, all invoices of a particular Business must be listed.

How to implement this like:

 #views.py
    #views.py
class BusinessDashboard(BusinessOwnerRequiredMixin, DetailView):
    model = Business
    template_name = "business/business-main.html"

class InvoiceListView(BusinessDashboard, ListView):
    model = Invoice
    template_name = "business/purchase/purchase_invoice-main.html"

class InvoiceDetailView(InvoiceListView, DetailView):
    model = Invoice
    template_name = "business/purchase/purchase_invoice.html"

But that woin't work, since I am overriding the model on each class...

For the url http://example.com/business/1/invoices/1/, inside the template I must have a variable with a invoice instance.

Upvotes: 0

Views: 290

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600059

There's no need for inheritance; you just need to define get_queryset to filter the invoices by the business pk.

class InvoiceListView(ListView):
    template_name = "business/purchase/purchase_invoice-main.html"

    def get_queryset(self):
        return Invoice.objects.filter(business_id=self.kwargs['pk'])

Upvotes: 2

Related Questions