views:

182

answers:

1

I have an admin class which uses raw_id_fields. Instead of displaying the number key, I would like to convert that to the __unicode__ for the corresponding foreign key object.

I thought a way to do this would be to add a form to the admin class. This form would be one in which the field I want to change is overridden with my own widget.

As an intermediate step, I just want to duplicate the existing behavior - so the raw id field would be displayed with its adjacent icon. I have a form with a widget of ForeignKeyRawIdWidget. This widget needs an argument though and I don't know what to give it. I have tried ForeignKeyRawIdWidget(DBObjectName.objects.get(pk=53)), for example, but I get the error DBObjectName object has no attribute 'to'.

I can see from its definition in related.py that the widget needs a to argument, but I don't know what it is looking for.

A: 

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.

KRH