0

我有一些具有一些 ForeignKey 关系的模型。

通过 forms.py 我添加了一个ModelMultipleChoiceField小部件

class VariableFieldForm(ModelForm):
    custom_field = forms.ModelMultipleChoiceField(queryset=VariableField.objects.all(), widget=FilteredSelectMultiple("Custom Fields", is_stacked=False))

    class Meta:
        model = VariableField

并且在尝试访问已保存的记录localhost:8000/admin/product/subcategory/3/或保存新记录时,我得到以下回溯

File "/opt/python2.6/lib/python2.6/site-packages/django/utils/encoding.py" in force_unicode
  71. s = unicode(s)
File "/opt/python2.6/lib/python2.6/site-packages/django/forms/forms.py" in __unicode__
  408. return self.as_widget()
File "/opt/python2.6/lib/python2.6/site-packages/django/forms/forms.py" in as_widget
  439. return widget.render(name, self.value(), attrs=attrs)
File "/opt/python2.6/lib/python2.6/site-packages/django/contrib/admin/widgets.py" in render
  39. output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choices)]
File "/opt/python2.6/lib/python2.6/site-packages/django/forms/widgets.py" in render
  581. options = self.render_options(choices, value)
File "/opt/python2.6/lib/python2.6/site-packages/django/forms/widgets.py" in render_options
  531. selected_choices = set([force_unicode(v) for v in selected_choices])

Exception Type: TemplateSyntaxError at /admin/product/subcategory/3/
Exception Value: Caught TypeError while rendering: 'long' object is not iterable

我正在使用 Django 1.3 和 MySQL

关于如何解决它的想法?

4

1 回答 1

0

最终是我使用了 ForeignKey,而不是 ManyToManyField 关系。

希望这有助于任何未来的问题

于 2011-10-20T12:08:40.753 回答