I have a three-levels Invoice
model which I'd like to display on Django's admin area... in a "kind of special" way.
Allow me to provide a bit of background:
Each Invoice
is conformed by several SubInvoice
(s), and each SubInvoice
is conformed by several InvoiceItem
(s), which contain a break down of the Products
purchased by a customer.
Logically speaking, it'd be something like this (hopefully the ascii art works)
+---------- Invoice id=3 -----------+
| Full total: $100.00 |
| |
| +----- Sub Invoice id=1 -----+ |
| | Subtotal $70 | |
| | | |
| | Item 1 in SubInv.1 | |
| | Item 2 in SubInv.1 | |
| | Item 3 in SubInv.1 | |
| |____________________________| |
| |
| +----- Sub Invoice id=2 -----+ |
| | Subtotal $30 | |
| | | |
| | Item 1 in SubInv.2 | |
| | Item 2 in SubInv.2 | |
| |____________________________| |
| |
|___________________________________|
The models look more or less (they've been simplified for this question) like:
class Invoice(models.Model):
full_total = DecimalField(...)
# has a .sub_invoices RelatedManager through a backref from SubInvoice
class SubInvoice(models.Model):
sub_total = DecimalField(...)
invoice = ForeignKey('server.Invoice', related_name='sub_invoices')
# has an .items RelatedManager through a backref from InvoiceItem
class InvoiceItem(models.Model):
sub_invoice = ForeignKey('server.SubInvoice', related_name='items')
product = ForeignKey('server.Product', related_name='+')
quantity = PositiveIntegerField(...)
price = DecimalField(...)
Now, I am aware that nesting two levels of relationships in Django Admin is very complex, and I'm not trying to nest the InvoiceItem
into the SubInvoice
and nest that one into the Invoice
. That'd be great, but I'm ready to give that up due to the difficulties of nested inlines. No: what I'd like to to do is showing the Invoice
and, as an inline
, its Items
, "jumping" through Invoice.sub_invoices__items
. I don't really care that much about the information shown in the SubInvoice
(s), but I do care about the information in the Invoice
and in the InvoiceItems
.
What I mean is that, basically, I would like (or "I could live with", rather) if the Invoice
admin view looked like the following:
+---------- Invoice id=3 -----------+
| Full total: $100.00 |
| |
| +----------------------------+ |
| | | |
| | Item 1 in SubInv.1 | |
| | Item 2 in SubInv.1 | |
| | Item 3 in SubInv.1 | |
| | Item 1 in SubInv.2 | |
| | Item 2 in SubInv.2 | |
| |____________________________| |
| |
|___________________________________|
(InvoiceItems
as an inline of the Invoice
(s) without showing any information about the SubInvoices
in it)
I've tried the following in the admin.py
:
class InvoiceItemInline(admin.StackedInline):
fk_name = 'sub_invoice__invoice'
model = InvoiceItem
class InvoiceAdmin(admin.ModelAdmin):
inlines = (InvoiceItemInline,)
But that gives me an error:
<class 'server.admin.invoices.InvoiceItemInline'>: (admin.E202) 'server.InvoiceItem' has no field named 'sub_invoice__invoice'.
I've also tried directly this:
class InvoiceItemInline(admin.StackedInline):
model = InvoiceItem
class InvoiceAdmin(admin.ModelAdmin):
inlines = (InvoiceItemInline,)
But then (this one I was expecting) produces this error:
<class 'server.admin.invoices.InvoiceItemInline'>: (admin.E202) 'server.InvoiceItem' has no ForeignKey to 'server.Invoice'.
Is there any way of achieving this? Thank you in advance.
PS:
As of now, I have a "patched" solution which seems to be the canonical way:
- Register the
Invoice
model. - Register an
admin.ModelAdmin
inline for theSubInvoice
(this inline will be "inlined" into theInvoice
's ModelAdmin). - Also register the
SubInvoice
in the admin, so we can calculate a link to its admin view. - Add an inline view of the
InvoiceItems
to the aforementionedSubInvoice
's view. - Add a link to the admin view of the
SubInvoice
(s) in theInvoice
Pretty much what is described in this other S.O. answer.
But the problem with this approach is that it won't let me see the Invoice
and its InvoiceItems
at a glance (I see the invoice, with sub_invoices in it, and then within the sub_invoices inlines, there's a link to the InvoiceItems which I have to click on in order to see the items). It'd be great if I could get rid of the need for that link.
This is what I have now, basically:
+---------- Invoice id=3 -----------+
| Full total: $100.00 |
| |
| +----- Sub Invoice id=1 -----+ | +--- Sub Invoice id=1 ---+
| | Subtotal $70 | | | Item 1 in SubInv.1 |
| | | | | Item 2 in SubInv.1 |
| | <a>Click for items ==============> | Item 3 in SubInv.1 |
| |____________________________| | |________________________|
| |
| +----- Sub Invoice id=2 -----+ |
| | Subtotal $30 | | +--- Sub Invoice id=2 ---+
| | | | | Item 1 in SubInv.2 |
| | <a>Click for items ==============> | Item 2 in SubInv.2 |
| |____________________________| | |________________________|
| |
|___________________________________|
I think your problem could be solved using the
ManyToManyField
+through
. (This is an example)I would recommend working with classes for this one in your
views.py
and useinlineformset_factory
in yourforms.py
for this. Usingjquery-formset
library in your frontend as well, as this looks pretty neat together.NOTE: You can also use
on_delete=models.CASCADE
if you want in theInvoiceItem
on the foreignkeys, so if one of those items is deleted, then the InvoiceItem will be deleted too, ormodels.SET_NULL
, whichever you prefer.Hope this might help you out.