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()
如何将检索到的日期字段和时间字段值合并到日期时间字段中以将其存储在数据库中。我有一个名为datetime_reading的文件。在我的页面中,我分别输入日期和时间字段。如何将日期和时间字段合并到单个日期时间字段中以将其存储在DB中的datetime_reading列中
答案 0 :(得分:9)
要合并日期和时间,您需要使用datetime.combine
datetime.combine(date, time)
答案 1 :(得分:0)
尝试在你的时间使用第一个strftime:
utime = strftime.time("......")
然后:
datetime.datetime.combine(date, utime)
答案 2 :(得分:0)
使用此
form1.datetime_reading = datetime.combine(date, time)