django对象不能与自定义实例进行迭代

时间:2013-12-12 18:12:17

标签: django forms instance iterable

我试图让我的对象本身脱离可能选项的查询集。问题是我得到错误:'Country'对象不可迭代 不知道我哪里出错了。
我的观点:

def edit_country(request, country_id):
    country = get_object_or_404(Country, pk=country_id)
    country_form = CountryForm(instance=country)
    return render(request, 'create_country.html', {'country_form': country_form})

我的表单 init

def __init__(self, *args, **kwargs):
    super(CountryForm, self).__init__(*args, **kwargs)
    if 'instance' in kwargs:
        self.fields['likes'].queryset = Country.objects.exclude(kwargs['instance'])
        self.fields['hates'].queryset = Country.objects.exclude(kwargs['instance'])

我哪里出错?

1 个答案:

答案 0 :(得分:0)

更改方法的顺序,因此首先弹出kwarg。你派kwarg去超级。

def __init__(self, *args, **kwargs):
    instance = kwargs.pop('instance', None)
    #all other stuff