Django: Show report earnings and expenses of a Django management system

Asked

Viewed 326 times

-1

I have a management system in Jango and would like to save in one place the report of all winnings (Paid=True), defaults (Paid=False) and expenses of the month or period in Jango admin itself, in a template or in a PDF document.

class MovDiario(models.Model):
    cliente = models.ForeignKey('Cliente', on_delete=models.CASCADE)
    veiculo = models.ForeignKey('Veiculo', on_delete=models.CASCADE)
    placa = models.CharField(max_length=10)
    servico = models.ManyToManyField('Servico')
    valor = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    pagamento = models.ForeignKey('FormPagamento', on_delete=models.CASCADE, blank=True, null=True)
    pago = models.BooleanField(default=False)
    entregue = models.BooleanField(default=False)

I want to know mainly how I can access the sum of all amounts paid to make a financial statement

  • The best way you can get help on the site is by putting the code of what you’ve done and where you’re struggling. Check the Help Center for a good question: https://answall.com/help/how-to-ask

1 answer

0


Hello live a way to get the sum, it’s like this:

In Python 2:

from django.db.models import Sum
total = MovDiario.objects.filter(pago=True).aggregate(Sum('valor')).values()[0]

In Python 3:

from django.db.models import Sum
valor_pago = list(MovDiario.objects.filter(pago=True).aggregate(Sum('valor')).values())[0]
  • I don’t quite understand where I should put this part of the code, you’d like to see the full code?

  • You have a save() function somewhere to update your Movdiario():, right? I don’t have access to your code, if you want to share a repository you publish if you have nothing that can’t be shared. You can always create a new model (table), with Aggregates, thus aggregating all customers with total paid, pending and delivered payment, but simplifying. In the admin you can add a package https://docs.djangoproject.com/pt-br/3.0/howto/outputting-pdf/, here you will create a function that loads values for PDF and here you use the code I shared.

  • Another good example of a PDF tutorial on Django Admin...https://medium.com/code-rocket-blog/gerando-pdf-a-partir-de-html-no-admin-do-django-6f7c3528f5b3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.