【发布时间】:2011-02-21 15:23:59
【问题描述】:
我正在尝试替换 Admin 中的默认 ManyToManyField 小部件以使用 FilteredSelectMultiple,但遇到了一些问题。我的错误是Caught TypeError while rendering: unpack non-sequence
这是我的模型
class Car(models.Model):
parts = models.ManyToManyField('Part')
class Part(models.Model):
name = models.CharField(max_length=64)
这是我的 ModelAdmin
class CarAdmin(admin.ModelAdmin):
form = myforms.CustomCarForm
这是 CustomCarForm
class CustomCarForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(CustomCarForm, self).__init__(*args, **kwargs)
parts = tuple(Part.objects.all())
self.fields['parts'].widget = admin.widgets.FilteredSelectMultiple(
'Parts', False, choices=parts
)
当我尝试查看管理员表单时收到此错误
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/foo/car/1/
Django Version: 1.2.5
Exception Type: TemplateSyntaxError
Exception Value: Caught TypeError while rendering: unpack non-sequence
Exception Location: /usr/lib/python2.4/site-packages/Django-1.2.5-py2.4.egg/django/forms/widgets.py in render_options, line 464
Python Executable: /usr/bin/python
Python Version: 2.4.3
如果我使用其中任何一个设置parts,则会出现此错误
parts = Part.objects.all()
parts = list(Part.objects.all())
parts = tuple(Part.objects.all())
如果我这样做
parts = Part.objects.value_list('name')
我收到Caught ValueError while rendering: need more than 1 value to unpack
编辑:如果我取出choices=parts,它会渲染得很好,但是选择框是空白的,我需要在其中添加一些东西。
【问题讨论】: