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