Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

881
Visualizações
Get all objects with today date, django

I have a model like this

class Maca(models.Model):
   created_at = models.DateTimeField(
     auto_now_add=True
   )

Now I want in the views.py file to get all the entries that have created today,

I'm trying this

Maca.objects.filter(created_at=datetime.today().date())

But this looks for the clock that object is created too.

P.S I can't change the field in model because I need the clock too in other purposes.

Can someone help me to select all entries that have been created today?

Thanks in advance

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

there many ways to get today data from database.

year =  datetime.datetime.now().strftime('%y')
month =  datetime.datetime.now().strftime('%m')
day =  datetime.datetime.now().strftime('%d')
filtereddata = Maca.objects.filter(created_at__year=year,created_at__month=month, created_at__day=day )

the second way is to filter with contains

filtereddata = Maca.objects.filter(created_at__contains=datetime.today().date())

if datetime.datetime not work. just remove one datetime only use single datetime

over 4 years ago · Santiago Trujillo Relatório

0

I think the main reason is because you trying to compare dt field with date.

You can use prefixes for a field.

Or you can compare your 'today' value as dt(just an example):

today = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
records = Maca.objects.filter(created_at >= today)
over 4 years ago · Santiago Trujillo Relatório

0

You have to just write a valid filter like this :

from datetime import datetime

today = datetime.today()

year = today.year
month = today.month
day = today.day

meca = Meca.objects.filter(created_at__year=year, 
created_at__month=month, created_at__day=day)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda