1

我有一个使用 raw_id_fields 的管理类。我不想显示数字键,而是将其转换__unicode__为对应的外键对象。

我认为这样做的一种方法是向管理类添加一个表单。此表单将是我想要更改的字段被我自己的小部件覆盖的表单。

作为中间步骤,我只想复制现有行为 - 因此原始 id 字段将与其相邻的图标一起显示。我有一个带有小部件的表单ForeignKeyRawIdWidget。这个小部件需要一个参数,但我不知道该给它什么。ForeignKeyRawIdWidget(DBObjectName.objects.get(pk=53))例如,我尝试过,但我得到错误 DBObjectName object has no attribute 'to'。

我可以从它在related.py 中的定义中看到该小部件需要一个 to 参数,但我不知道它在寻找什么。

4

1 回答 1

0

It's looking for an argument in the form of Class._meta.get_field('field to link').rel.

For example, I have a class called Photo whose image field is a ForeignKey to another class, and I want that ForeignKey's widget to appear different. The argument that the ForeignKeyRawIDWidget wants for rel is Photo._meta.get_field('image').rel.

于 2009-06-15T21:29:38.800 回答