django orden por conjunto de consultas, ascendente y descendente


¿Cómo puedo ordenar descendiendo mi consulta establecida en django por fecha?

Reserved.objects.all().filter(client=client_id).order_by('check_in')

Solo quiero filtrar desde descendente todos los Reservados por fecha check_in.

Author: iTayb, 2012-03-23

8 answers

Reserved.objects.filter(client=client_id).order_by('-check_in')

Observe el - antes de check_in.

Documentación de Django

 363
Author: Keith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-18 03:06:12
Reserved.objects.filter(client=client_id).order_by('-check_in')

Un guion "-" delante de "check_in" indica el orden descendente. El orden ascendente está implícito.

No tenemos que añadir un all() antes de filter(). Eso todavía funcionaría, pero solo necesitas agregar all() cuando quieras todos los objetos del QuerySet raíz.

Más sobre esto aquí: https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-specific-objects-with-filters

 51
Author: Leonardo.Z,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-20 18:10:39

Para el orden ascendente:

Reserved.objects.filter(client=client_id).order_by('check_in')

Para el orden descendente:

1.  Reserved.objects.filter(client=client_id).order_by('-check_in')

O

2.  Reserved.objects.filter(client=client_id).order_by('check_in')[::-1]
 11
Author: Anjaneyulu Batta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-25 12:46:14

También puede usar la siguiente instrucción:

Reserved.objects.all().filter(client=client_id).order_by('check_in').reverse()
 10
Author: Patrick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-04 12:26:01

Funciona eliminando .all():

Reserved.objects.filter(client=client_id).order_by('-check_in')
 9
Author: PaVen Nguyen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-21 09:07:26

Añadiendo el - se ordenará en orden descendente. También puede establecer esto agregando un orden predeterminado a la meta de su modelo. Esto significará que cuando hagas una consulta solo harás MyModel.objeto.all () y saldrá en el orden correcto.

class MyModel(models.Model):

    check_in = models.DateField()

    class Meta:
        ordering = ('-check_in',)
 2
Author: Thomas Turner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-26 14:43:45

Esto está funcionando para mí.

latestsetuplist = SetupTemplate.objects.order_by('-creationTime')[:10][::1]
 0
Author: Manish Gupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-26 17:10:35
Reserved.objects.filter(client=client_id).order_by(*check_in)
 -2
Author: 飞翔的企鹅,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-30 03:56:17