1 from django import forms
2 from django.conf import settings
5 class ModelLookupWidget(forms.TextInput):
8 def __init__(self, content_type, attrs=None):
9 self.content_type = content_type
10 super(ModelLookupWidget, self).__init__(attrs)
12 def render(self, name, value, attrs=None):
13 related_url = '../../../%s/%s/' % (self.content_type.app_label, self.content_type.model)
16 if not attrs.has_key('class'):
17 attrs['class'] = 'vForeignKeyRawIdAdminField'
18 output = super(ModelLookupWidget, self).render(name, value, attrs)
19 output += '<a href="%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);">' % (related_url, name)
20 output += '<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, _('Lookup'))
23 value_class = self.content_type.model_class()
25 value_object = value_class.objects.get(pk=value)
26 output += ' <strong>%s</strong>' % escape(truncate_words(value_object, 14))
27 except value_class.DoesNotExist:
29 return mark_safe(output)