Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Django tables 2 creating custom calculated fields
def calculateTotalVists(days):
    total = 0
    for i in days:
        total += i
    return total

class ClientTable(tables.Table):
    class Meta:
        model = Client
        fields = ('id', 'name', 'phone')
        attrs = {'class': 'table table-striped table-bordered', 'id': 'clients'}

The client model has the field "days" which is a list. I would like to create a custom column which displays the result after days is passed into calculateTotalVists(). However I'm not how I can use accessors with functions.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can't use a random function and expect tables2 to magically pass it the appropriate arguments taken form the table record. Just make it a method (or property) on the client model:

# models.py
class Client(models.Model):
    # ...
    def total_visits(self):
        return sum(self.days)  # or whatever self.days-based calculation

# tables.py
from django_tables2.utils import A

class ClientTable(tables.Table):
    total_visits = Column(
        accessor=A('total_visits'),
        # ...
    )
    class Meta:
        model = Client
        fields = ('id', 'name', 'phone')
        sequence = ('id', 'name', 'phone', 'total_visits')
        # ...
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!