[Answered ]-Django adds extra columns to group by

2๐Ÿ‘

โœ…

I took a look at TimeStampedModeland it set a default ordering in the class meta:

class TimeStampedModel(models.Model):
""" TimeStampedModel
An abstract base class model that provides self-managed "created" and
"modified" fields.
"""
created = CreationDateTimeField(_('created'))
modified = ModificationDateTimeField(_('modified'))

class Meta:
    get_latest_by = 'modified'
    ordering = ('-modified', '-created',)
             # ^^^^^^^^^^^^^^^^^^^^^^^^^^
    abstract = True

see in github

You could overwrite that providing another filter to order by (like tipo__nombre or monto), ex:

Pago.objects.filter(
    created__range=(self.inicio, self.fin)
).values(
    'tipo__nombre'
).annotate(
    monto=Sum('monto')
).order_by(
    'tipo__nombre'
)
๐Ÿ‘คRenan Ivo

Leave a comment