模型.py
class KEBReading(models.Model):
#id = models.AutoField(primary_key=True,db_column='kr_id')
date=models.DateField()
time=models.TimeField()
truepower_reading=models.DecimalField(max_digits=6,
decimal_places=2,blank=False,null=False)
apparentpower_reading=models.DecimalField(max_digits=6,
decimal_places=2,blank=False,null=False)
truepower_consumed=models.DecimalField(max_digits=6, decimal_places=2)
voltage_reading=models.IntegerField(blank=False,null=False)
powerfactor=models.DecimalField(max_digits=3,decimal_places=2)
表格.py
class KEBReading_form(forms.ModelForm):
class Meta:
model=KEBReading
exclude=("truepower_consumed","powerfactor")
widgets = {
"date" : forms.widgets.DateInput(format="%d/%m/%Y"),
"time" : forms.widgets.TimeInput(format="%H:%M")
}
视图.py
def KEBReading1(request):
if request.method == "POST":
form = KEBReading_form(request.POST)
if form.is_valid():
prevdate=KEBReading.objects.latest("date")
print prevdate.date
print prevdate.time
# q1 = KEBReading.objects.get(datetime.date.today()-datetime.timedelta(0))
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)
form.save()
KEBReading.objects.filter().values("date","time","truepower_reading",
"apparentpower_reading","truepower_consumed","voltage","powerfactor")
q2=KEBReading.objects.latest("date")
context={'KEBReading_form':form,'q2':q2}
return render_to_response('keb.html',context,context_instance=RequestContext(request))
else:
form = KEBReading_form()
return render_to_response('keb.html',{'KEBReading_form':form},context_instance=RequestContext(request))
我想将 truepower_consumed aftr 计算保存在数据库中。但它给了我一个错误 truepower_consumed 列不能为空。我在表格中排除了 truepower_consumed。但在数据库中我需要保存该字段