Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django filter between two date

My django model datetime field is string. In this case, how to get data between two dates?

models.py

class asdf(models.Model):
     recordDate = models.CharField(max_length=20,blank=True)
like image 311
Anar Ali Avatar asked Nov 30 '22 14:11

Anar Ali


2 Answers

Change the 'recordDate' to DateField and use the following query:

asdf.objects.filter(recordDate__gte='2019-03-01', recordDate__lte='2019-03-09')
like image 85
Raj Kumar Avatar answered Dec 04 '22 04:12

Raj Kumar


In order to get between range use filter this query:

models.asdf.objects.filter(recordDate__gte='start_date', recordDate__lt='end_date')

start_date and end_date may be string in date formats or datetime parsed object.

like image 41
Aalok kumar Avatar answered Dec 04 '22 03:12

Aalok kumar