[英]How do you save multitype/form data to a hard file in Python with FastAPI UploadFile?
[英]How do you save this form data?
这是我的forms.py:
class MatchingForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MatchingForm, self).__init__(*args, **kwargs)
for matchq in MatchQuestions.objects.all():
self.fields[matchq.question] = forms.ModelChoiceField(queryset=Choice.objects.filter(question=matchq.id))
self.fields[matchq.howImportant] = forms.ChoiceField(choices=((1,"Very Important"),(2,"Not Important"),))
如您所见,此表单遍历数据库中的数据。 如何在我的视图中遍历这些动态表单字段,以便将它们保存到每个表单中? 谢谢
基本思想是覆盖表单的save方法,并迭代cleaned_data
class MatchingForm(forms.Form):
[...]
def save(self):
for question, howImportant in self.cleaned_data.items():
obj, created = MatchQuestions.objects.get_or_create(question=question,
howImportant=howImportant)
PD:这是一个示例,因为我不知道您的数据模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.