Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

123
Vistas
how to filter posts for a specific hour from its publishing time

I'm trying to show my posts for a specific time here is my models.py

class Booking(models.Model):
    check_in = models.DateTimeField(default=timezone.now)
    check_out = models.DateTimeField(blank=True,null=True)

i want display object for 12 hours after check out for example if we have an object its check out is 14/10/2021 10AM then show that post till 10PM !

i tried it , but didnt work Booking.objects.filter(check_out__hour=12)

is it possible please ? thank you in advance

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can filter with:

from datetime import timedelta
from django.db.models.functions import Now

Booking.objects.filter(
    check_out__gte=Now()-timedelta(hours=12)
)

you can filter further such that no objects with a check_out that is later than now are filtered out with a __range lookup [Django-doc]:

from datetime import timedelta
from django.db.models.functions import Now

Booking.objects.filter(
    check_out__range=(Now()-timedelta(hours=12), Now())
)

Here Now() is the timestamp of the database.

We can also work with the timestamp of the webserver machine with:

from datetime import timedelta
from django.utils.timezone import now

Booking.objects.filter(
    check_out__range=(now()-timedelta(hours=12), now())
)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda