0

Basically I need to achieve below thing: when user enters start date and end date , i should be able to auto populate no of days (excluding saturday, sunday and holidays) in one of the field in the form. At database level I have table and query ready for calculating this thing. how to incorporate the same in django?

I have Leave application and three files: models.py,views.py,forms.py and leave.html file

class Leaves(models.Model):
    leaveID = models.AutoField(primary_key=True)
    fromDate = models.DateField()
    toDate = models.DateField()
    leaveType=models.CharField(max_length=20)
    noofDays = models.IntegerField(blank=True,null=True)
    createdBy = models.CharField(max_length=30)
    createdAt = models.DateTimeField(default=datetime.datetime.now, null=True, blank=True)
    updatedAt = models.DateTimeField(default=datetime.datetime.now, null=True, blank=True)


def leave(request):
    context = {'form': Leaves}
    if request.method=='POST':
        form = LeaveForm(request.POST)

        if form.is_valid():
            leaveID=request.POST.get('leaveID')
            fromDate=request.POST.get('fromDates')
            toDate=request.POST.get('toDate')
            leaveType = request.POST.get('leaveType')
            createdBy = User.objects.get(username=request.user.username)
            noofDays = Dim_Date.objects.raw('select count(distinct calendarDate) from Dim_Date where isWeekday=1 and isHoliday<>1')
            form.save()

            return render(request,'leave/leaveconfirmation.html')
        else:
            return render(request,'leave/leave.html')
    else:
        return render(request,'leave/leave.html',context)


I should be able to auto populate field based on from_date and to_date given in the form. (Backend logic is ready) Please let me know how to integrate the same in django.

Vidya
  • 3
  • 4
  • use pandas to calculate number of days (https://stackoverflow.com/questions/2224742/most-recent-previous-business-day-in-python) and pass value to form – HenryM May 15 '19 at 13:51
  • Done..Now able to populate no of days automatically based on database calculation. – Vidya May 16 '19 at 16:22

0 Answers0