Home > Enterprise >  how to get filter data between two dates in django
how to get filter data between two dates in django

Time:10-04

I am working on Django project How to get filter data between two dates I mean (from-date and to-date) mfdate is a foreign key and coming from another model. how can I do this can anyone help me to solve this problem I have tried multiple time but it is not working

here is my Code

models.py

 class ManufacturingYM(models.Model):
     ManufacturingYMID = models.AutoField(primary_key=True)
     ManufacturingDate = models.DateField(auto_now_add=False, blank= True,null=True)

     def __str__(self):
         return str(self.ManufacturingDate)

  class Car(models.Model):
      CarID = models.AutoField(primary_key=True)
      CarName = models.CharField(max_length=500, blank=True, null=True, verbose_name="Car Name")
      mfdate = models.ForeignKey(ManufacturingYM, blank= True,null=True, on_delete=models.DO_NOTHING, verbose_name="Manufacturing Date")

      def __str__(self):
          return self.CarName

views.py

   def searchdd(request):

      carForm = Car.objects.all()
      fromdate = str(request.POST.get('from_date'))
      todate = str(request.POST.get('to_date')) 

      if fromdate:    
          carForm= Car.objects.filter(mfdate=fromdate)    
      if todate:
          carForm= Car.objects.filter(mfdate=todate)

      context = {'carForm':carForm} 
      return render(request, 'app/searchdd.html', context)

home.html

     <form method="post" id="indexForm" action="/searchdd" data-cars-url="{% url 'ajax_load_cars' %}">
    {% csrf_token %}
  <div class="col-md-12">
    <div class="row">
      <div class="col-md-3">
        <label for="" class="white">From Year</label>
        <select name="from_date" id="fromdate" class="dp-list">
          <option disabled selected="true" value="">--select Year--</option>
          {% for mfd in manfd %}
          <option value="{{car.CarID}}">{{mfd.ManufacturingDate|date:'Y'}}</option>
          {% endfor %}
        </select>
      </div>
      <div class="col-md-3">
        <label for="" class="white">To Year</label>
        <select name="to_date" id="todate" class="dp-list">
          <option disabled selected="true" value="">--select Year--</option>
          {% for mfd in manfd %}
          <option value="{{car.CarID}}">{{mfd.ManufacturingDate|date:'Y'}}</option>
          {% endfor %}
        </select>
      </div>
      </div>
      </div>

CodePudding user response:

If from_date and get_date have as format YYY-MM-DD, then you can filter with:

def searchdd(request):
      carForm = Car.objects.all()
      fromdate = request.POST.get('from_date')
      todate = request.POST.get('to_date')

      if fromdate:    
          carForm = carForm.filter(mfdate__ManufacturingDate__gte=fromdate)
      if todate:
          carForm = carForm.filter(mfdate__ManufacturingDate__lte=todate)

      context = {'carForm':carForm} 
      return render(request, 'app/searchdd.html', context)

If the request does not contain a from_date or to_date, then that filter is omitted. If both are thus omitted, all Cars will be returned. If to_date is 2021-1-1, then you will retrieve all Cars that have been manufactued before or on January 1st, 2021.

As for the form, you should format the items correctly, so:

<select name="from_date" id="fromdate" class="dp-list">
    <option disabled selected="true" value="">--select Year--</option>
        {% for mfd in manfd %}
            <option value="{{ mfd.ManufacturingDate.year }}-1-1">{{mfd.ManufacturingDate|date:'Y'}}</option>
        {% endfor %}
</select>

the same for the to_date, but then it is {{ mfd.ManufacturingDate.year }}-12-31.

  • Related