I want to bring the date of all the records and compare it with the current date?

1

Hi! I want to be able to bring the date of all my records to compare it with the current date to know how many days a certain product has been registered but I do not know how to do it, please help me.

I do not know if I make myself understood is something like

fechaactual = Datetime.now()

fecharegistro = Empaque.objects.values('fecha')

But when it comes to showing it, it only shows me the last one

    
asked by Angie Lizeth Santiago Piñeros 28.09.2016 в 16:29
source

3 answers

2

What you need to do is:

Empaque.objects.filter(fecha__lt=Datetime.now())

You should look at this part of the documentation

link

    
answered by 28.09.2016 в 16:54
0
from datetime import datetime

fecha_actual = datetime.now()
registros = Empaque.objects.all()

for registro in registros:
    delta = fecha_actual - registro.fecha
    registro.dias_desde_registro = delta.days
    
answered by 28.09.2016 в 16:55
0

This is the solution for what I was looking for I hope it serves someone.

These is my view:

def inventarioempaque(request):
        empaque = Empaque.objects.all().order_by('fecha')
        fecha = Empaque.objects.values('fecha')

    #Codigo para calcular fecha actual
    fechaa = strftime("%Y-%m-%d")
    #Codigo para calcular hora actual
    hora = strftime("%H:%M:%S")

    fecha_actual = date.today()
    registros = Empaque.objects.all().order_by('fecha')

    for obj in empaque:
        delta = fecha_actual - obj.fecha
        obj.dias_desde_registro = delta.days

    return render(request,"inventario/empaque.html",{'empaque': empaque});

This is my template:

<div class="container">
    <div class="row">
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Fecha empaque</th>
                        <th>Hora</th>
                        <th>Variedad</th>
                        <th>Grado</th>
                        <th>Empaque</th>
                        <th>Comercializadora</th>
                        <th>Cantidad de ramos</th>
                        <th>Unidades por ramo</th>
                        <th>Celda</th>
                        <th>Dias en empaque</th>
                    </tr>
                </thead>
                <tbody>
                    {% for obj in empaque %}
                    {%if obj.dias_desde_registro >= 12 %} 
                    <tr style="background-color: #FC3041">
                        <th>{{obj.fecha}}</th>
                        <th>{{obj.hora}}</th>
                        <th>{{obj.variedad}}</th>
                        <th>{{obj.grado}}</th>
                        <th>{{obj.empaque}}</th>
                        <th>{{obj.comercializadora}}</th>
                        <th>{{obj.ramos}}</th>
                        <th>{{obj.unidades}}</th>
                        <th>{{obj.celda}}</th>
                        <th >{{obj.dias_desde_registro}}</th>
                    {% else %}
                        <th>{{obj.fecha}}</th>
                        <th>{{obj.hora}}</th>
                        <th>{{obj.variedad}}</th>
                        <th>{{obj.grado}}</th>
                        <th>{{obj.empaque}}</th>
                        <th>{{obj.comercializadora}}</th>
                        <th>{{obj.ramos}}</th>
                        <th>{{obj.unidades}}</th>
                        <th>{{obj.celda}}</th>
                        <th >{{obj.dias_desde_registro}}</th>
                    {% endif %}
                </tbody>
                {% endfor %}
            </table>
        </div>
    </div>
</div>
    
answered by 28.09.2016 в 17:41