这是我的表格代码:
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
的 in 中get_form()
,在第 0 步,我用ProviderForm
上面看到的 my 加载数据,然后在第 2 步,我使用以下代码将数据加载到 my 中ChoiceField
,并尝试使用我在第 0 步使用的值过滤数据:
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
我得到了错误:
'QuerySet' object has no attribute '_meta'
看起来对于 Django 我尝试处理多个模型
那么我该如何过滤我的choicefield消费者的数据呢?