Saturday, January 22, 2022

Re: order_by on the basis of time difference (updated_at - created_at)

you can use datetime.datetime.strptime to avoid this error. I resolved it few days back.
for this you have to import datetime like this: import datetime

On Sat, Jan 22, 2022 at 12:19 PM Lalit Suthar <sutharlalit.97@gmail.com> wrote:

Fabio C. Barrionuevo da Luz's answer will work like a charm!




On Fri, 21 Jan 2022 at 21:04, Fabio C. Barrionuevo da Luz <bnafta@gmail.com> wrote:
this can be easily solved by using an annotation to calculate the difference and store it in a new temporary column, and then sort by it


from django.db.models import F
from myapp.models import ExampleModel

queryset = ExampleModel.objects.annotate(
    dt_difference=F('updated_at') - F('created_at')
).order_by('dt_difference')

for example in queryset[:10]:
    print(f'created_at={example.created_at} - updated_at={example.updated_at} - dt_difference={example.dt_difference}')



Em sex., 21 de jan. de 2022 às 10:54, Gabriel Araya Garcia <gabrielaraya2011@gmail.com> escreveu:
Try this:
ExampleModel.objects.all().order_by('updated_at' , '-created_at')
Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos




El vie, 21 ene 2022 a las 1:54, Aadil Rashid (<aadil10121998@gmail.com>) escribió:
class ExampleModel(models.Model):
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)



I want to query on UserModel such that the Query set which I get should be orderable in terms of time difference of updated_at - created_at,
I tried
ExampleModel.objects.all().order_by('updated_at' - 'created_at')
but this is not working
it throws, TypeError: unsupported operand type(s) for -: 'str' and 'str'


Django Family Please Help...................

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAYXZx8sABKuJn5Q1ATeHnYsrixfU9wTH_TtPYoR5%3DeAd8DOzA%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKVvSDDy%2BnJNcFE-xpz38zfDVYgw6P3zDZ9BJ_5-EsoAtDDLTw%40mail.gmail.com.


--
Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPVjvMYVR1yqZjLuEh5u6DaR5DZfeYqvFy-MaNkt7MiWO7O%2Bxw%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGp2JVEnnVbHDXN2tkQjKK1R3JYF19GY94Ax%3D91OYrKykL39_w%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKPY9pkuhrAmw_5aFmuEee8gBueSGG9hmg%3DA_e9qoama07dCcA%40mail.gmail.com.

No comments:

Post a Comment