0

I want to add extra-column which is not in my model. And I apply one of the solutions at this site to my project. But it doesn't work properly.

model.py

class Companies(models.Model):

    legal_name      = models.CharField(max_length=120, blank=True)
    co_name         = models.CharField(max_length=120, blank=True)
    client          = models.ForeignKey(Clients, models.SET_NULL, blank=True, null=True)
    tel_no          = models.CharField(max_length=120, blank=True)
    email           = models.EmailField(null=True, blank=True)
    address         = models.TextField(null=True, blank=True)
    contact         = models.CharField(max_length=250, blank=True)
    con_tel_no      = models.CharField(max_length=120, blank=True)
    entity          = models.CharField(max_length=2, null=True)
    yearend         = models.DateField(null=True, blank=True)
    bn              = models.CharField(max_length=9)
    memo            = models.TextField(null=True, blank=True)
    slug            = models.SlugField(null=True, blank=True)

    def t2_due_date(self):
        now_year = datetime.date.today().year
        if self.entity == 'CO':
            yearend_ = DateWidget.decompress(self, self.yearend)
            if yearend_[1] > 6:
                yearend_[2] = now_year + 1
                yearend_[1] -= 6
            else:
                yearend_[2] = now_year
                yearend_[1] += 6
            t2_due = DateWidget.compress(self, yearend_)
        return t2_due

tables.py

class ScheduleTable(tables.Table):
    due_date_col = tables.Column(accessor='t2_due_date', verbose_name='T2 Due Date')

    class Meta:
        attrs = {"class": "paleblue", "width":"100%"}
        fields = ['client','legal_name', 'co_name', 'entity', 'yearend', 'due_date_col']
        model = Companies

When I run this program 'due_date_col' is always blank. It seems that the function('t2_due_date) does not go through. Do you have any clue to clear this problem?

Jieter
  • 4,101
  • 1
  • 19
  • 31
TS Jee
  • 121
  • 1
  • 4
  • 12
  • Please sort your code examples out. The indentation is wrong! Also there is no need to align the equal signs, just follow [PEP 8](https://www.python.org/dev/peps/pep-0008/). – cezar Jan 31 '18 at 13:25
  • Class names are by convention singular, thus `Company` is better than `Companies`. – cezar Jan 31 '18 at 13:26
  • Thank you for the comments. – TS Jee Jan 31 '18 at 16:44

1 Answers1

0

As far as I know accessor points to related objects, rather than model's properties, methods etc.

What you can try is to make use of Table.render_{column} as so:

class ScheduleTable(tables.Table):

    def render_due_date_col(self, record):
        return record.t2_due_date()

See djanog tables official doc for more info.

Tomb
  • 71
  • 1
  • 9
  • I tried your suggestion but the model's method does not work at all yet. Is there any other way using 'extra_columns'? I read many answers about this and they are not clear to me. – TS Jee Jan 31 '18 at 18:06
  • Did you try to trace the method with pdb to see that its being called? Regardless, make sure you Remove the accessor if you use custome renderer. – Tomb Feb 01 '18 at 06:37
  • I used to use 'print' to check. And no print output from models.py while I can see the final view output. Do you expect any difference if I try pdb? – TS Jee Feb 01 '18 at 17:58
  • I would first print something in the `render_*` method to see that the table actually calls its `Column.render_` method. – Tomb Feb 02 '18 at 15:01
  • I put 2 'print's in the Meta class and the render_* method. The former works while the latter does not. – TS Jee Feb 02 '18 at 17:08
  • I found that "accessor" worked actually. The problem was there were some errors in "t2_due_date" function and this function was ignored. I expect that even if the function has some errors, the function should be executed popping up an error message. But interestingly it wasn't. – TS Jee Feb 05 '18 at 04:18