误差函数,问题出在哪里,python,django,django-models,django-forms,django-views"/>

首页 > 解决方案 > Python 和 Django ,误差函数,问题出在哪里

问题描述

我有以下课程:

class Income(models.Model):
  income = models.DecimalField()
  date = models.DateField()

在我看来,我创建了一个算法,可以让我每月收集所有日期,如下所示:

now = datetime.datetime.now()
now = now.year
income = dict()
for year, month, totals in (Income.objects.values_list('date__year', 'date__month').annotate(totals = ExpressionWrapper(Sum(F('income')),
    output_field = FloatField())).values_list('date__year', 'date__month', 'totals')):
  if id not in income.keys() and year == now:
  income[id] = list(defaults)
  index = month - 1
  income[id][index] = totals

如果我现在按以下方式设置,它会完美运行:

now=datetime.datetime.now()
now=now.year

但是,如果我以下列方式设置表单:

#
models

class Timing(models.Model):
  reference_year = models.DateField()

# views

  nows = []
  form = TimingForm(request.POST or None)
  if request.method == 'POST':
    if form.is_valid():
      nows = form.cleaned_data['reference_year']
  nows = nows.year

Python给了我以下错误:

<built-in function id>

income[id][index]=totlas

为什么??

标签: pythondjangodjango-modelsdjango-formsdjango-views

解决方案


推荐阅读