Я использую общие представления DetailView
и ListView
У меня есть три модели User
, Business
и Invoice
. Пользователь может иметь несколько предприятий, может иметь несколько счетов.
#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')
Я ищу наследование ListView Invoice от DetailView of Business, т. Е. От экземпляра Business, т. Е. Все счета определенного Business должны быть перечислены.
Как это реализовать, например:
#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"
Но это не сработало, так как я переопределяю model
в каждом классе ...
Для URL http://example.com/business/1/invoices/1/
внутри шаблона у меня должна быть переменная с экземпляром счета.