0

I have the following models:

class Client(models.Model):
    ...

class Document(models.Model):
    client = models.ForeignKey(Client, related_name='clients')
    ...

class MainVoiceActual(models.Model):
    doc = models.ForeignKey(Document, related_name='documents')
    value = models.IntegerField()

My objective is to print in a template the total of all values belonging to a document belonging to a specific client. To do this I'd like to add a calc_total method to my Client model.

I tried the following but it does not work:

class Client(models.Model):
    name = models.CharField(max_length=150)

    def calc_total(self):
        docs = Document.objects.filter(client=self)

        return sum(doc.mainvoiceactuals_set.value for doc in docs)

I get a 'Document' object has no attribute 'mainvoiceactuals_set' error

davideghz
  • 3,596
  • 5
  • 26
  • 50

1 Answers1

0

Got it working with:

def calc_total(self):
    voices = MainVoiceActual.objects.filter(doc__client=self)
    return sum(voice.value for voice in voices)
davideghz
  • 3,596
  • 5
  • 26
  • 50