1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why no results filtering dates using a foreignkey in Django?

Discussão em 'Python' iniciado por Stack, Novembro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I need to filter a variable between two models using a foreign key.

    models.py

    class Vuelo(models.Model):
    fecha_de_vuelo = models.CharField(max_length=50)
    .......

    class Novedad(models.Model):
    fecha_de_vuelo_novedad = models.ForeignKey(Vuelo, on_delete=models.CASCADE, related_name="fecha_de_vuelo_novedad", null=True, editable=False)
    comandante_novedad = ......


    view.py

    def buscar_reporte_demoras(request):
    if request.method == "GET":

    inicio = request.GET.get("fecha_inicio", None)
    fin = request.GET.get("fecha_fin", None)

    criterio_1 = Q(fecha_de_vuelo_novedad__fecha_de_vuelo__gte = inicio)
    criterio_2 = Q(fecha_de_vuelo_novedad__fecha_de_vuelo__lte = fin)

    busqueda = Novedad.objects.all()
    busqueda_con_filtro_fechas = busqueda.filter(criterio_1 & criterio_2)

    context = {
    'lista_vuelos': busqueda_con_filtro_fechas,
    'criterio_fecha_inicio': inicio,
    'criterio_fecha_fin': fin,
    }

    return render(request, '.........html', context)


    Why doesn't it give me any results when I try to filter by "fecha_de_vuelo" using related_name = "fecha_de_vuelo_novedad"?

    Continue reading...

Compartilhe esta Página