From a84031405e9b3656ff92fc2f29716e0d7c519006 Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Thu, 15 Aug 2019 23:08:15 +0200 Subject: [PATCH] create Custom history table --- apps/note/models/transactions.py | 4 ++++ apps/note/tables.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 apps/note/tables.py diff --git a/apps/note/models/transactions.py b/apps/note/models/transactions.py index 11be33f1..42eff212 100644 --- a/apps/note/models/transactions.py +++ b/apps/note/models/transactions.py @@ -106,6 +106,10 @@ class Transaction(models.Model): self.destination.save() super().save(*args, **kwargs) + @property + def total(self): + return self.amount*self.quantity + class MembershipTransaction(Transaction): membership = models.OneToOneField( diff --git a/apps/note/tables.py b/apps/note/tables.py new file mode 100644 index 00000000..4d4e9608 --- /dev/null +++ b/apps/note/tables.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +import django_tables2 as tables +from .models.transactions import Transaction + + +class HistoryTable(tables.Table): + class Meta: + attrs = {'class':'table table-bordered table-condensed table-striped table-hover'} + model = Transaction + template_name = 'django_tables2/bootstrap.html' + sequence = ('...','total','valid') + + total = tables.Column() #will use Transaction.total() !! + + def order_total(self, QuerySet, is_descending): + # needed for rendering + QuerySet = QuerySet.annotate( + total=F('amount') * F('quantity') + ).order_by(('-' if is_descending else '') + 'total') + return (QuerySet, True)