Issue with model method

22 views
Skip to first unread message

Felix Lazaro Carbobonell

unread,
Dec 10, 2018, 9:06:06 AM12/10/18
to django...@googlegroups.com

 

Good day to everyone.

 

I have defined a model method wich is called on a template (DTL) and the result is None when I test the URI with selenium. Normal attributes from the model are displayed correctly.

But when I test the same URI with the Django test client it works as expected and the result from the model method is correctly rendered in the template.

 

Could you please help me with this issue.

 

Best regards,

Felix Carbonell.

Yarving Liu

unread,
Dec 11, 2018, 9:46:07 AM12/11/18
to django...@googlegroups.com
Can you show us the code?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/018e01d49091%24608e8e80%2421abab80%24%40epepm.cupet.cu.
For more options, visit https://groups.google.com/d/optout.

Felix Carbonell

unread,
Dec 12, 2018, 2:41:31 PM12/12/18
to Django users


El martes, 11 de diciembre de 2018, 9:46:07 (UTC-5), Yarving Liu escribió:
Can you show us the code?
 
Hello and thanks for your response:

{{ ccosto.gastos_mes }} and {{ ccosto.gastos_ano }} yield None

Nevertheless if I print the response content when calling that URI with django test client it shows the expected values.

## models.py

from django.db import models
from django.db.models import Q, Sum
from datetime import datetime

# Create your models here.


class CentroCosto(models.Model):
    ocostcodigo = models.IntegerField(db_column='OCostCodigo', primary_key=True)  # Field name made lowercase.
    ocostdescrip = models.TextField(db_column='OCostDescrip', blank=True)  # Field name made lowercase.

    @property
    def codigo(self):
        return self.ocostcodigo

    def listar_gastos(self):
        return self.gastos_set(manager='gastado').all()

    def gastos_mes(self, mes=datetime.today().month):
        _gastos = self.listar_gastos().filter(smaycosfechamodif__month=mes).aggregate(Sum('smaycospartidaimporte'))
        return _gastos['smaycospartidaimporte__sum']

    def gastos_ano(self, ano=datetime.today().year):
        _gastos = self.listar_gastos().filter(smaycosfechamodif__year=ano).aggregate(Sum('smaycospartidaimporte'))
        return _gastos['smaycospartidaimporte__sum']


class GastosManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(Q(smaycoscuenta=702) | Q(smaycoscuenta=703)
                                             | Q(smaycoscuenta=731) | Q(smaycoscuenta=822))


class Gastos(models.Model):
    smaycosid = models.DecimalField(db_column='SMayCosId', max_digits=18, decimal_places=0, primary_key=True)
    smaycoscuenta = models.SmallIntegerField(db_column='SmayCosCuenta', blank=True, null=True)
    smaycospartidaimporte = models.DecimalField(db_column='SMayCosPartidaImporte', max_digits=19, decimal_places=4, blank=True, null=True)
    smaycossubelem = models.IntegerField(db_column='SMayCosSubelem', blank=True, null=True)
    smaycosocostocodigo = models.ForeignKey(CentroCosto, on_delete=models.CASCADE)
    smaycosfechamodif = models.DateTimeField(db_column='SMayCosFechaModif', blank=True, null=True)


    objects = models.Manager()
    gastado = GastosManager()

    @property
    def codigo(self):
        return self.smaycosocostocodigo
       
    @property
    def importe(self):
        return self.smaycospartidaimporte
   

## views.py       
       
from django.views import generic
from .models import Gastos
from .models import CentroCosto

# Create your views here.


class CentrosDeCostoListView(generic.ListView):
    template_name = 'gastos/centrosdecosto.html'
    context_object_name = 'centroscosto'
    model = CentroCosto
   

## centrosdecosto.html
   
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Resumen Centros de Costo</title>
</head>
<body>
  <table>
      <thead>
        <tr>
            <th>Centro de Costo</th>
            <th>Mes Actual</th>
            <th>Acumulado</th>
        </tr>
      </thead>
      {% for ccosto in centroscosto %}
        <tr>
            <td>{{ ccosto.codigo }}</td><td>{{ ccosto.gastos_mes }}</td><td>{{ ccosto.gastos_ano }}</td>
        </tr>
      {% endfor %}
  </table>
</body>
</html>
Reply all
Reply to author
Forward
0 new messages