When selecting a foreignkey in the django admin change form I am trying to add an href that can view the record next to the plus that adds the record.
What I've tried just to get the href to render is I've copied out the admins def render into my own custom widgets file and added it to and subclassed it:
widgets.py
class RelatedFieldWidgetWrapperLink(RelatedFieldWidgetWrapper):
def render(self, name, value, *args, **kwargs):
rel_to = self.rel.to
info = (rel_to._meta.app_label, rel_to._meta.object_name.lower())
try:
related_url = reverse('admin:%s_%s_add' % info, current_app=self.admin_site.name)
except NoReverseMatch:
info = (self.admin_site.root_path, rel_to._meta.app_label, rel_to._meta.object_name.lower())
related_url = '%s%s/%s/add/' % info
self.widget.choices = self.choices
output = [self.widget.render(name, value, *args, **kwargs)]
if self.can_add_related:
# TODO: "id_" is hard-coded here. This should instead use the correct
# API to determine the ID dynamically.
output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
(related_url, name))
output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="%s"/></a>' % (settings.ADMIN_MEDIA_PREFIX, _('Add Another')))
output.append(u'<a href="%s" class="testing" id="add_id_%s" onclick="#"> ' % \
(related_url, name))
return mark_safe(u''.join(output))
and in admin.py
formfield_overrides = {models.ForeignKey:{'widget':RelatedFieldWidgetWrapperLink}}
however I get thefollowing error:
TypeError init() takes at least 4 arguments (1 given)
Has anyone run into this problem before?