我有一个 django formtool:它需要插入名称并从 MultipleChoiceField 中选择 N 个值的第一步。当用户在第二步中提交此表单时,我需要显示 N MultipleChoiceField,其中每个 MultipleChoiceField 的每个元素都是从数据库中检索的。
我需要获取第一步表单的值,从 Db 中检索数据并将这些数据发送到第二步(forms.py)。对于第二步,我想我需要一个表单集。我已经阅读了一些关于将值从视图传递到表单的问题,但任何解决方案。这是我的代码:
表格.py
from django import forms
from station.models import Station
from django.forms import formset_factory
from .models import Vdl
station_all=Station.objects.all()
class VdlSelectStationsForm(forms.Form):
vdl_name = forms.CharField(label='nome virtual data logger', max_length=20,
widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'nome vdl'}))
stations = forms.ModelMultipleChoiceField(queryset=station_all)
class VdlSelectSensorsForm(forms.Form):
sensor_list_for_station = forms.ModelMultipleChoiceField(queryset=Vdl.objects.all()) #filter(id_user = id_user))
VdlSelectSensorsForm = formset_factory(VdlSelectSensorsForm, extra=1)
视图.py
from django.shortcuts import render
from .forms import VdlSelectStationsForm, VdlSelectSensorsForm
from formtools.wizard.views import SessionWizardView
class VdlWizard(SessionWizardView):
template_name = "vdl.html"
form_list = [VdlSelectStationsForm, VdlSelectSensorsForm]
def get_form_initial(self, step):
initial = {}
# If at second step, add image path to initial data for canvas field
if step == '1':
first_step_data = self.storage.get_step_data('0')
print('first_step_data ', first_step_data['0-stations'])
form = VdlSelectSensorsForm(initial={"form_field": first_step_data})
def initial_value(request):
my_values = [1, 2, 3]
return my_values
initial_value(request, my_values)
return self.initial_dict.get(step, initial)
def parse_params(self, request, *args, **kwargs):
current_step = self.determine_step(request, *args, **kwargs)
if request.method == 'POST' and current_step == 0:
form = self.get_form(current_step, request.POST)
if form.is_valid():
self.initial[(current_step + 1)] = {
'vdl_name': form.cleaned_data['vdl_name'],
'stations': form.cleaned_data['stattions'],
}
def done(self, form_list, **kwargs):
form_data = process_form_data(form_list)
context = {
'form_data': form_data,
}
return render(self.request, 'vdl.html', {'form_data': [form.cleaned_data for form in form_list],})
def process_form_data(form_list):
form_data = [form.cleaned_data for form in form_list]
return form_data
vdl.html
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
{% for field in form %}
{{field.error}}
{% endfor %}
<form action="" method="post">
{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="Invia"/>
</form>
{% endblock %}
模型.py
from django.db import models
class Vdl(models.Model):
id_vdl = models.IntegerField(primary_key=True, unique=True)
vdl_name = models.CharField(blank=False, max_length=20)
id_vdl_station = models.CharField(blank=False, max_length=100)
id_user = models.IntegerField()
class Meta:
db_table = 'vdl'