2011-02-09 89 views
1

我正在构建基于ManyToManyRawIdWidget的自己的小部件。Django,重写ManyToManyField

但只要我尝试在我的ModelAdmin如下:

formfield_overrides = { 
    ManyToManyField: {'widget': ManyToManyRawIdWidget}, 
} 

它会提示我

__init__() takes exactly 2 arguments (1 given) 

我想我需要填补rel争论的东西,可能是关系模型(这是一个Tag模型)

但后来我碰上TemplateSyntaxError:

Caught AttributeError while rendering: type object 'Tag' has no attribute 'to' 

这是我失去监督的地方。有人可以帮我吗?

回答

0

只是想确保你听从这个警告documentation for formfield_overrides

Warning

If you want to use a custom widget with a relation field (i.e. ForeignKey or ManyToManyField), make sure you haven't included that field's name in raw_id_fields or radio_fields.

formfield_overrides won't let you change the widget on relation fields that have raw_id_fields or radio_fields set. That's because raw_id_fields and radio_fields imply custom widgets of their own.

我不知道ManyToManyRawIdWidget的,而是由它的名字判断我认为raw_id_fields可能在这种情况下被设置?这可能是它不工作的原因。

+0

不,我遇到了这个警告,因此谨慎地从raw_id_fields中删除了该字段。没有运气那个:( – Kasper 2011-02-10 08:05:49