这是我的表格代码:
class ServiceChoiceForm(forms.ModelForm):
def activated_services(self):
"""
get the activated services added from the administrator
"""
all_datas = ()
data = ()
services = ServicesActivated.objects.filter(status=1)
for class_name in services:
data = (class_name, class_name.name.rsplit('Service', 1)[1])
all_datas = (data,) + all_datas
return all_datas
class ProviderForm(ServiceChoiceForm):
provider = forms.ChoiceField()
def __init__(self, *args, **kwargs):
super(ProviderForm, self).__init__(*args, **kwargs)
self.fields['provider'].choices = self.activated_services()
class Meta:
model = TriggerService
fields = ('provider', )
class ConsummerForm(ServiceChoiceForm):
consummer = forms.ChoiceField()
class Meta:
model = TriggerService
fields = ('consummer', )
在views.py
的{{1}}中,在第0步,我使用我的get_form()
加载数据,就像我们在上面看到的那样,然后在第2步,我使用以下代码加载我的ProviderForm
中的数据,并尝试使用我在步骤0中使用的值过滤数据:
ChoiceField
但是当我访问def get_form(self, step=None, data=None, files=None):
if step == '2':
data = self.get_cleaned_data_for_step('0')
consummer = ServicesActivated.objects.filter(status=1)
consummer = consummer.exclude(name__exact=data['provider'])
form = ConsummerForm(instance=consummer)
时,我收到错误:
ConsummerForm
看起来对于Django来说,我尝试处理多个模型
那么如何过滤我的选择字段消费者的数据呢?
答案 0 :(得分:1)
consummer = ServicesActivated.objects.filter(status=1)
consummer = consummer.exclude(name__exact=data['provider'])
form = ConsummerForm(instance=consummer)
您正在将QuerySet传递给表单而不是模型实例: https://docs.djangoproject.com/en/1.5/topics/forms/modelforms/