0

视图.py

            AppTruepower = form.cleaned_data['truepower_reading']
            AppApparent = form.cleaned_data['apparentpower_reading']
            AppVoltage = form.cleaned_data['voltage_reading']
            AppDate = form.cleaned_data['Date']
            AppTime = form.cleaned_data['Time']
            prevdate=KEBReading.objects.latest("id")
            print prevdate.truepower_reading
            print id
            KEBReading.objects.filter(id=id).update(truepower_reading=AppTruepower,apparentpower_reading=AppApparent,
                                                        voltage_reading=AppVoltage,datetime_reading=datetime.combine(AppDate,AppTime))


            a=KEBReading.objects.filter(id=id).values("truepower_reading")
            print a
            b=KEBReading.objects.filter(id=id).values("apparentpower_reading")
            print b
            print prevdate.truepower_reading

在 django 表单中编辑一行时。我需要访问前一行来做一些计算。sumbody 可以建议我如何获得上一行。如果我使用 model.s.object.latest() 我会输入最后一行,但在我的情况下,我需要获取上一行,即我要编辑的当前行的前一行。

4

1 回答 1

0

我取决于您对行的含义是什么顺序。如果你想要上一行,id你可以做

curr_row = KEBReading.objects.filter(id=current_id)
#decrement id until you find valid record, as previous id might have deleted.
prev_row = KEBReading.objects.filter(id=current_id-1) 

如果您想按其他字段排序,那么

rows = KEBReading.objects.order_by('-some_field_to_order') # use - for reverse ordering
current_row = rows[0]
prev_row = rows[1]  
#verify for index errors though.
于 2013-02-15T11:44:19.760 回答