gpt4 book ai didi

django - 将 form_list 表单实例保存到数据库的正确方法?

转载 作者:行者123 更新时间:2023-12-04 20:38:02 26 4
gpt4 key购买 nike

我正在使用 SessionWizardView 在多个页面上划分一个小调查。

我的问题是调查的每一页的数据都保存在我的数据库中的不同行中,其中一页上有多个输入字段,这些信息保存在同一行中。这使我的数据库非常麻烦。

enter image description here

我将此问题隔离到我的 save()每次有 form 时都会执行的函数在我的 form_list ,因此每次都在数据库中添加新行。

def done(self, form_list, **kwargs):

for form in form_list:
form.save()

return render(self.request, 'Return_to_Prolific_Academic.html', {
'form_data': [form.cleaned_data for form in form_list],
})

我试图通过 form_list.save() 直接在 form_list 上执行保存但我收到错误:

'list' object has no attribute 'save'



我也试过 join()chain()看看我是否可以把所有的 form 连接在一起实例,看看我是否可以同时保存它们但没有成功。

我想简化我当前的解决方案,以便每个参与者的数据都保存在数据库中的一行中,有人能告诉我如何去做吗?谢谢

编辑

我的一些models.py和forms.py的例子

模型.py
class Person(models.Model):
....
....

sender = models.EmailField(null=True, blank=True, verbose_name='What is your email address?')

birthdate = models.DateField(null=True, blank=True) #overwritten in forms.py so passed no more arguments

SEX = (
('1', 'Male'),
('2', 'Female'))
sex = models.CharField(null=True, blank=True, max_length=100, choices=SEX, verbose_name='What sex are you?')

MARITAL_STATUS = (
('SINGLE', "Single"),
('INARELATIONSHIP', "In a relationship"),
('MARRIED', "Married"),
('DIVORCED', "Divorced"),
('SEPARATED', "Separated"),
('WIDOWED', "Widowed"),)
marital_status = models.CharField(null=True, blank=True, max_length=100, choices=MARITAL_STATUS, verbose_name='What is your marital status?')

INTERNET_USAGE = (
('INTERNET_LESS_THAN_ONE_HOUR_A_DAY', 'Less than one hour a day'),
('INTERNET_ONE_TO_TWO_HOURS_A_DAY', '1 - 2 hours a day'),
('INTERNET_TWO_TO_FOUR_HOURS_A_DAY', '2 - 4 hours a day'),
('INTERNET_FOUR_TO_SIX_HOURS_A_DAY', '4 - 6 hours a Day'),
('INTERNET_SIX_TO_EIGHT_HOURS_A_DAY', '6 - 8 hours a day'),
('INTERNET_EIGHT_PLUS_HOURS_A_DAY', '8 + hours a day'), )
internet_usage = models.CharField(null=True, blank=True, max_length=100, default=None, choices=INTERNET_USAGE)

SMART_PHONE_OWNERSHIP = (
('YES_SMARTPHONE', 'Yes'),
('NO_SMARTPHONE', 'No'),)
smart_phone_ownership = models.CharField(null=True, max_length=100, blank=True, choices=SMART_PHONE_OWNERSHIP, verbose_name='Do you own a Smartphone?')

表格.py
class SurveyFormIT1(forms.ModelForm):      
class Meta:
model = Person
fields = ['instruction_task_one_value', 'instruction_task_one_image']
widgets = {'instruction_task_one_value' : forms.HiddenInput,
'instruction_task_one_image' : forms.HiddenInput}

class SurveyFormIT2(forms.ModelForm):
class Meta:
model = Person
fields = ['instruction_task_two_value', 'instruction_task_two_image']
widgets = {'instruction_task_two_value' : forms.HiddenInput,
'instruction_task_two_image' : forms.HiddenInput}

class Start(forms.ModelForm):
class Meta:
model = Person
fields = ['start']
widgets = {'start' : forms.HiddenInput}

class SurveyFormA(forms.ModelForm):

birthdate = forms.DateField(widget=extras.SelectDateWidget(years=range(1920, 1998)), required=True)

class Meta:
model = Person
fields = ['prolific_academic_ID', 'sender', 'birthdate', 'sex', 'marital_status', 'state']

class SurveyFormB(forms.ModelForm):
class Meta:
model = Person
fields = ['internet_usage', 'smart_phone_ownership', 'smart_phone_usage']
widgets = {'internet_usage' : RadioSelectNotNull,
'smart_phone_ownership' : RadioSelectNotNull,
'smart_phone_usage' : RadioSelectNotNull,}

最佳答案

super 简单, super 笨拙的方式,因为我不知道你的数据模型:

# Accumulate all the filled-in data:
data = {}
for form in form_list:
for k,v in form.__dict__.items():
data[k] = v if v else data.get(k)

# Save it to the first form
form_list[0].__dict__.update(data)
form_list[0].save()

关于django - 将 form_list 表单实例保存到数据库的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31751609/

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