gpt4 book ai didi

python - __init__() 获得意外的关键字参数 'wellness'

转载 作者:行者123 更新时间:2023-12-01 04:45:29 25 4
gpt4 key购买 nike

虽然标题说问题是“健康”,但事实是它在表格上随机选择一个项目并说这就是问题。

surveys.views.py

from django.shortcuts import render
from surveys.forms import SurveyForm
from django.contrib.auth.decorators import login_required

# Create your views here.

@login_required
def storeBloodData(request):
if request.method == 'POST':
form = SurveyForm(request.POST)

if(form.is_valid()):
cd = form.cleaned_data
bd = SurveyForm (user=cd['user'],
timestamp=cd['timestamp'],
glucose=cd['glucose'],
wellness=cd['wellness'],
weight=cd['weight'],
foodGroups=cd['foodGroups'],
)
bd.save()
print("Saved weather record...")
else:
return render(request,
'bdata_form.html',{'form':form})

print("should be calling status...")
return render(request, 'welcome.html',{'term':"Saved data to d/base..."})

surveys.forms.py

from django import forms
from surveys.models import Survey
from django.conf import settings

FOOD_CHOICE = {
('D','Diary'),
('F','Fruit'),
('G','Grains'),
('M','Meats'),
('V','Vegetables'),
('S','Sweets'),
}

class SurveyForm(forms.ModelForm):

class Meta:
model = Survey
fields = ( 'glucose',
'weight', 'foodGroups',
'wellness', 'user',
'timestamp'
)

surveys.models.py

from django.db import models
from userprofile.models import UserProfile
from django.conf import settings

from django.contrib.auth.models import User




# Create your models here.

FOOD_CHOICE = {
('D','Diary'),
('F','Fruit'),
('G','Grains'),
('M','Meats'),
('V','Vegetables'),
('S','Sweets'),
}


NUM_CHOICE = {
('1','1'),
('2','2'),
('3','3'),
('4','4'),
('5','5'),
('6','6'),
('7','7'),
('8','8'),
('9','9'),
('10','10'),
}

class Survey(models.Model):
user = models.ForeignKey(User, related_name='Survey.user')
glucose = models.DecimalField(max_digits=3, decimal_places=0)
timestamp = models.DateTimeField('date published', default='1990-08-26')
weight = models.DecimalField(max_digits=5, decimal_places=2)
foodGroups = models.CharField(max_length=1)
wellness = models.CharField(max_length=2, choices=NUM_CHOICE, default="1")

def __str__(self):
return self.user

更多信息因此它正确加载表单(并且我能够从管理端添加调查),但每当我尝试从网站端添加它时,它都会抛出错误。

最佳答案

您正在尝试在 is_valid block 内实例化 SurveyForm,而不是调查。

但实际上你不应该尝试这样做,并且你不需要从 clean_data 设置所有这些字段:使用 ModelForm 的全部意义在于你可以只执行 form.save() 它会为您创建并保存一个模型实例。

关于python - __init__() 获得意外的关键字参数 'wellness',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29500012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com