Greetings to all.
I am using a queryset
to display the data in a table indicating the records created in each month and how many of them are men and how many are women.
This is my model:
GENDER_CHOICES = (
('', _('')),
('M', _("Masculino")),
('F', _("Femenino")),
('O', _("otro")))
class PersonaBase(models.Model):
# Otros atributos
genero = models.CharField(max_length=1, null=True, blank=False,
choices=GENDER_CHOICES, verbose_name=_("genero"))
class TimeStampedModel(models.Model):
fecha_creacion = models.DateTimeField(auto_now_add=True)
class Simpatizante(TimeStampedModel, PersonaBase):
# Otros atributos
I am using the following statement:
num_simpatizates_meses_qs = Simpatizante.objects.values('fecha_creacion', 'genero') \
.annotate(num_simpatizantes_meses=Count('fecha_creacion')) \
.annotate(hombres_simpatizantes_meses=Count(Case(When(genero='M',
then=(Simpatizante.objects.filter(genero='M').count()))))) \
.annotate(mujeres_simpatizantes_meses=Count(Case(When(genero='F',
then=(Simpatizante.objects.filter(genero='F').count()))))) \
.filter(campana_electoral=campana_electoral).order_by('-fecha_creacion')
I tried to do it using a @property
in the model sympathizer but it didn't work:
@property
def mes_fecha_creacion(self):
return self.fecha_creacion.strftime("%B")
Currently I get a list with the datetime
creation of each one.
But I need to display it grouped by months, January, February, March, etc.
I appreciate the help they can give me
I solved it in the following way:
And with the following line I get the name of the corresponding month:
Where
mes['month']
is the number corresponding to the month I need