2011-02-09 139 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