How to get the values from the table based on dates from datetime field django
views.py
from django.shortcuts import render
import pymysql
from django.http import HttpResponseRedirect
from facligo_app.models import Scrapper
from django.utils import timezone
import datetime
import pytz
find_description = ""
def index(request):
if request.method == "POST":
from_date = request.POST.get("from_date")
print(from_date)
to_date = request.POST.get("to_date")
print(to_date)
roles = Scrapper.objects.all()
# for i in roles:
# print(i.description)
global find_description
find_description = Scrapper.objects.all().filter(created_at=from_date,updated_at=to_date)
print(find_description)
return render(request, "home.html", {"scrappers": find_description})
models.py
from django.db import models
from django.db import connections
# Create your models here.
class Scrapper(models.Model):
id = models.IntegerField(primary_key=True)
full_name = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
description = models.TextField(max_length=100)
created_at = models.DateTimeField(default=True)
updated_at = models.DateTimeField(default=True)
class Meta:
db_table = "test_table_1"
From my form I will post data where my from_date = "2022-11-09" and to_date = "2022-11-09". But my table column has time with date. Is there any solution to get the data based on only date I post. But my table should not make any alter.
First convert the the given date to datetime object using strptime
Example
from_date = request.POST.get("from_date")
# For a datetime.datetime obj:
from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d')
Then you can use to filter.
find_description = Scrapper.objects.all().filter(created_at__date=from_date,updated_at__date=to_date)