Tôi có một FormWizard, nơi tôi cần dữ liệu từ biểu mẫu đầu tiên chuyển tới hàm tạo của biểu mẫu thứ hai để tôi có thể tạo biểu mẫu động.Cách chuyển dữ liệu biểu mẫu trước đó tới hàm tạo của DynamicForm trong FormWizard
Tôi có thể lấy dữ liệu của biểu mẫu đầu tiên thông qua process_step của FormWizard.
Tôi tạo các trường của biểu mẫu thứ hai bằng cuộc gọi cơ sở dữ liệu của danh sách trường.
class ConditionWizardDynamicQuestions(forms.Form):
def __init__(self, DynamicQuestions=None, *args, **kwargs):
super(ConditionWizardDynamicQuestions, self).__init__(*args, **kwargs)
questions = Question.objects.filter(MYDATA = DATA_FROM_1STFORM)
for q in questions:
dynField = FieldFactory(q)
self.fields[q.label] = dynField
Làm thế nào tôi có thể vượt qua DATA_FROM_1STFORM?
đang kết quả của tôi: tôi từ bỏ init dạng, và chuyển nó vào CreateQuestions def. Sau đó sử dụng ghi đè get_form của wizard để thay đổi biểu mẫu sau khi tạo.
class ConditionWizard(SessionFormWizard):
def get_form(self, request, storage, step=None, data=None, files=None):
form = super(ConditionWizard, self).get_form(request, storage, step, data, files)
stepIndex = self.get_step_index(request, storage, step)
if stepIndex == 1:
form.CreateQuestions(request.session["WizardConditionId"])
if stepIndex == 3:
form.fields['hiddenConditionId'].initial = request.session["WizardConditionId"]
form.fields['medicationName'].queryset = Medication.objects.filter(condition = request.session["WizardConditionId"])
return form
Tôi đang gặp sự cố khi truyền dữ liệu từ bước 1 đến bước 3, nhưng giải pháp của bạn hữu ích và sâu sắc. – BozoJoe
Bạn đang gặp phải vấn đề gì? AFAIK nó sẽ cho phép bạn truy cập tất cả các dữ liệu từ tất cả các hình thức trước đó - đó là bước 3 nên có dữ liệu từ bước 1 và bước 2. –
vấn đề của tôi là tôi đã cố gắng sử dụng ban đầu từ trình hướng dẫn và ghi đè biểu mẫu __trong đó__. Tôi chuyển sang sử dụng get_form và thế giới đang trở nên rõ ràng hơn đối với tôi :) – BozoJoe