为什么'模型'对象不可迭代?

时间:2016-12-25 21:09:12

标签: python django django-forms django-admin

我尝试在管理员面板中添加一个radioselect 。我使用多对多字段来连接另一个模型的对象(背景)。

我现在拥有的内容:收音机选择在此处并在管理面板上运行,但在我继续并保存更改时出现此错误:'Background' object is not iterable

如何保存和保留所选信息?

MyApp的/的 models.py

class Background(models.Model):
    bk_color = models.CharField(max_length=20)
    ...

class FormOne(models.Model):
    name = models.CharField(max_length=40)
    background = models.ManyToManyField(Background, blank=True)
    ...

MyApp的/的 forms.py

class FormOneForm(forms.ModelForm):
    class Meta:
        model = FormOne
        fields = ['name', 'background']

    background = forms.ModelChoiceField(widget=forms.RadioSelect, queryset=Background.objects.all(), required=False)

MyApp的/的 admin.py

class FormOneAdmin(ModelAdmin):
    fields = ['name', 'background']
    form = FormOneForm


site.register(Background)
site.register(FormOne, FormOneAdmin)

我无法调试问题,我很高兴知道它的来源以及如何解决它?

编辑:

添加了TraceBack:

Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python34\lib\site-packages\django\contrib\admin\options.py" in wrapper
  618.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\contrib\admin\sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\contrib\admin\options.py" in change_view
  1521.         return self.changeform_view(request, object_id, form_url, extra_context)
File "C:\Python34\lib\site-packages\django\utils\decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\utils\decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Python34\lib\contextlib.py" in inner
  30.                 return func(*args, **kwds)
File "C:\Python34\lib\site-packages\django\contrib\admin\options.py" in changeform_view
  1470.                 self.save_related(request, form, formsets, not add)
File "C:\Python34\lib\site-packages\django\contrib\admin\options.py" in save_related
  1102.         form.save_m2m()
File "C:\Python34\lib\site-packages\django\forms\models.py" in save_m2m
  102.                 f.save_form_data(instance, cleaned_data[f.name])
File "C:\Python34\lib\site-packages\django\db\models\fields\related.py" in save_form_data
  2603.         setattr(instance, self.attname, data)
File "C:\Python34\lib\site-packages\django\db\models\fields\related.py" in __set__
  1266.         value = tuple(value)

Exception Type: TypeError at /admin/multistepform/formone/75/
Exception Value: 'Background' object is not iterable

1 个答案:

答案 0 :(得分:0)

您无法使用radio widget创建多对多关系,因为django期待一个background id数组(这就是django在您的堆栈跟踪中抱怨的内容)将数据发布到background添加视图时,而不是单个FormOneAdmin ID。

您可以注释掉ModelChoiceField的行,然后您就可以使用它了。

class FormOneForm(forms.ModelForm):
    class Meta:
        model = FormOne
        fields = ['name', 'background']

    # background = forms.ModelChoiceField(widget=forms.RadioSelect, queryset=Background.objects.all(), required=False)

或者,如果您仍然只需要让零个或Background个对象绑定到FormOne个对象,则应使用ForeignKey代替ManyToManyField。代码看起来像这样:

class FormOne(models.Model):
    name = models.CharField(max_length=40)
    # background = models.ManyToManyField(Background, blank=True)
    background = models.ForeignKey(Background, blank=True) # <-- use foreign key instead


class FormOneForm(forms.ModelForm):
    class Meta:
        model = FormOne
        fields = ['name', 'background']

    background = forms.ModelChoiceField(widget=forms.RadioSelect, queryset=Background.objects.all(), required=False)


class FormOneAdmin(admin.ModelAdmin):
    fields = ['name', 'background']
    form = FormOneForm