Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda