Changing the Template container node detection code to use the child_nodelists attrib...
[philo.git] / admin / widgets.py
1 from django import forms
2 from django.conf import settings
3
4
5 class ModelLookupWidget(forms.TextInput):
6         # is_hidden = False
7         
8         def __init__(self, content_type, attrs=None):
9                 self.content_type = content_type
10                 super(ModelLookupWidget, self).__init__(attrs)
11         
12         def render(self, name, value, attrs=None):
13                 related_url = '../../../%s/%s/' % (self.content_type.app_label, self.content_type.model)
14                 if attrs is None:
15                         attrs = {}
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'))
21                 output += '</a>'
22                 if value:
23                         value_class = self.content_type.model_class()
24                         try:
25                                 value_object = value_class.objects.get(pk=value)
26                                 output += '&nbsp;<strong>%s</strong>' % escape(truncate_words(value_object, 14))
27                         except value_class.DoesNotExist:
28                                 pass
29                 return mark_safe(output)