Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date and Time merging to datetimefield

Tags:

django

views.py 

def KEBReading1(request):


q2=KEBReading.objects.all().order_by('datetime_reading')[:30]

form = KEBReading_form()
monthform=MonthForm()
if request.method == "POST":
    if request.POST.get('KEBsubmit'):
        form = KEBReading_form(request.POST)
        if form.is_valid():
            try:
                prevdate=KEBReading.objects.latest("datetime_reading")    

                print "teena"             

                date=form.cleaned_data["Date"]
                print date
                time=form.cleaned_data["Time"]
                print time
                kr_truepower_reading = form.cleaned_data["truepower_reading"]
                kr_apparentpower_reading = form.cleaned_data["apparentpower_reading"]

                truepower_consumed1=kr_truepower_reading-prevdate.truepower_reading
                powerfactor1=((kr_truepower_reading-prevdate.truepower_reading)/(kr_apparentpower_reading-prevdate.apparentpower_reading))     
                form1=form.save(commit=False)
                form1.datetime_reading__date=date


                form1.datetime_reading___time=time
                print type(date)

                form1.truepower_consumed=truepower_consumed1
                form1.powerfactor=powerfactor1

                print form1.powerfactor


                form1.save()

how do i merge the date field and time field value i retrieved into a datetime field to store it the database. i have a filed called datetime_reading. and in my page i enter date and time field separately. how do i merge the date and time field into a single datetime field to store it in datetime_reading column in DB

like image 828
prithu Avatar asked Dec 06 '22 09:12

prithu


1 Answers

To merge date and time you need to use datetime.combine

datetime.combine(date, time)
like image 68
Efrin Avatar answered Dec 23 '22 12:12

Efrin