3

I'm trying to sum up all values from column using this documentation, but footer doesn't show up. I'm I missing something?

models.py

class Mokejimai(models.Model):
    id = models.AutoField(primary_key=True)
    nr = models.IntegerField(verbose_name='Mok. Nr.')
    data = models.DateField(verbose_name='Kada sumokėjo')
    suma = models.FloatField(verbose_name='Sumokėta suma')
    skola_pagal_agnum = models.FloatField(verbose_name='Skola pagal Agnum')
    date_entered = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name='Apmokėjimas įvestas')
    date_modified = models.DateTimeField(auto_now_add=False, auto_now=True, blank=True, null=True)
    imone = models.ForeignKey(Imones, models.DO_NOTHING, verbose_name='Įmonė')
    sask = models.ForeignKey(Saskaitos, blank=True, null=True, verbose_name='Sąskaita')
    user = models.ForeignKey(User, models.DO_NOTHING, default=settings.AUTH_USER_MODEL)

tables.py

class MokejimaiTable(tables.Table):
    suma = tables.Column(footer=lambda table: sum(x['suma'] for x in table.data))

    class Meta:
        model = Mokejimai
        attrs = {"class": "paleblue"}
        fields = ('id', 'imone', 'sask', 'nr', 'suma', 'skola_pagal_agnum', 'data', 'date_entered')
Gabrielius B.
  • 71
  • 2
  • 4
  • 12
  • Can you try with `suma = tables.Column(footer=lambda ...` by renaming it to something else? I am guessing, the same name of Model might be overriding it. So say use `suma_total = tables.Column(footer=lambda ..` and then add that also to fields: `fields = ('id', 'imone', 'sask', 'nr', 'suma', 'suma_total', 'skola_pagal_agnum` – Nagaraj Tantri Jun 08 '16 at 12:50
  • What version of django-tables2 do you use? Do you have a custom template without [tfoot block](https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/templates/django_tables2/table.html#L41-L51)? – Jieter Jun 09 '16 at 07:45
  • @NagarajTantri in `suma_total = ...` case it just adds additional column named "suma_total" – Gabrielius B. Jun 09 '16 at 10:29
  • @Jieter django-tables2 (1.2.1). My template is very simple: {% load render_table from django_tables2 %}
    {% render_table table %}
    – Gabrielius B. Jun 09 '16 at 10:31
  • My question was to check if you do not override the default `django_tables2/table.html` template. I just started to add a FAQ to the django-tables2 docs today, and added this example which works like I expect it to work: https://github.com/bradleyayers/django-tables2/blob/faq/tests/test_faq.py#L59-L71. – Jieter Jun 09 '16 at 11:40
  • @Jieter Not sure what do you mean. I'm quite new to Python and especially to Django. Could you elaborate? – Gabrielius B. Jun 09 '16 at 11:59
  • @GabrieliusB. just make sure you don't have a file called `table.html` in a folder called `django_tables2` in one of the template directories in your project and you should be fine on this point. – Jieter Jun 09 '16 at 13:13
  • I don't have neither `django_tables2` folder, neither `table.html` in my project. – Gabrielius B. Jun 09 '16 at 13:45
  • @Jieter do you have any ideas why it isn't working? In fact, now it's rendering a row, but not fully. Here's printscreen: http://oi67.tinypic.com/20gi9s3.jpg – Gabrielius B. Jun 10 '16 at 08:45

1 Answers1

3

Your screenshot shows that django-tables2 correctly assumes there is a footer on your table (yay!) but it seems that nothing is returned from the lambda. You can try to replace it by something like this to see what's going on:

def suma_footer(table):
    try:
        s = sum(x['suma'] for x in table.data)
        print 'total:', s
    except Exception e:
        print str(e)
        raise

    return s


class MokejimaiTable(tables.Table):
    suma = tables.Column(footer=suma_footer)

    class Meta:
        model = Mokejimai
        attrs = {"class": "paleblue"}
        fields = ('id', 'imone', 'sask', 'nr', 'suma', 'skola_pagal_agnum', 'data', 'date_entered')

If something goes wrong while computing the sum, you should see a exception printed, if a value is computed, you should see 'total: ' printed.

Jieter
  • 4,101
  • 1
  • 19
  • 31
  • The same partly rendered row. Terminal prints `'Mokejimai' object has no attribute '__getitem__'` error. Sample of `Mokejimai` MySQL table: http://oi66.tinypic.com/mwp095.jpg. Footer perfectly appears with this snippet `suma = tables.Column(footer="Total:")`. Ofcourse only without any calculations. – Gabrielius B. Jun 10 '16 at 10:25
  • Yeah, django templates eat the exception in the previous code example. I guess you need to use `sum(x.suma for x in table.data)` since it is a Django model not supporting accessing it's attributes with the dict-syntax. – Jieter Jun 10 '16 at 10:30
  • sum(x.suma for x in table.data) worked like a charm. Thx! – Gabrielius B. Jun 10 '16 at 10:33
  • Remember you can always verify such a piece of code works outside of the table. – Jieter Jun 10 '16 at 10:41
  • Understood. Thanks again. – Gabrielius B. Jun 10 '16 at 10:44