Ticket #4641: 4641_raw_admin_fields_display_fix.diff

File 4641_raw_admin_fields_display_fix.diff, 3.2 KB (added by Brian Rosner, 17 years ago)

more than likely the final patch to be committed. posted here to get some last minute feedback.

  • django/contrib/admin/tests/widgets.py

    diff --git a/django/contrib/admin/tests/widgets.py b/django/contrib/admin/tests/widgets.py
    index 0b20441..55d845c 100644
    a b WIDGET_TESTS = """  
    77>>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget
    88>>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
    99>>> from django.contrib.admin.models import LogEntry
     10>>> from django.contrib.auth.models import User
    1011
    1112Calling conditional_escape on the output of widget.render will simulate what
    1213happens in the template. This is easier than setting up a template and context
    HTML escaped.  
    2930>>> print conditional_escape(w.render('test', 'test'))
    3031Currently: <a target="_blank" href="%(MEDIA_URL)stest">test</a> <br>Change: <input type="file" name="test" />
    3132
     33>>> entry = LogEntry(action_flag=1)
     34>>> entry.user = User.objects.create_user("jdoe", "jdoe@example.com")
     35>>> entry.save()
    3236>>> rel = LogEntry._meta.get_field('user').rel
    3337>>> w = ForeignKeyRawIdWidget(rel)
    34 >>> print conditional_escape(w.render('test', 'test', attrs={}))
    35 <input type="text" name="test" value="test" class="vForeignKeyRawIdAdminField" /><a href="../../../auth/user/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>
     38>>> print conditional_escape(w.render('test', entry.user.pk, attrs={}))
     39<input type="text" name="test" value="..." class="vForeignKeyRawIdAdminField" /><a href="../../../auth/user/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>&nbsp;<strong>jdoe</strong>
    3640
    3741""" % {
    3842    'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
  • django/contrib/admin/widgets.py

    diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
    index 26aacf7..f5c14e6 100644
    a b Form Widget classes specific to the Django admin site.  
    44
    55from django import newforms as forms
    66from django.utils.datastructures import MultiValueDict
    7 from django.utils.text import capfirst
     7from django.utils.text import capfirst, truncate_words
    88from django.utils.translation import ugettext as _
    99from django.utils.safestring import mark_safe
    1010from django.conf import settings
    class ForeignKeyRawIdWidget(forms.TextInput):  
    100100        output.append('<a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> ' % \
    101101            (related_url, url, name))
    102102        output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>' % settings.ADMIN_MEDIA_PREFIX)
     103        if value:
     104            output.append('&nbsp;<strong>%s</strong>' % \
     105                truncate_words(self.rel.to.objects.get(pk=value), 14))
    103106        return mark_safe(u''.join(output))
    104         #if self.change: # TODO
    105             #output.append('&nbsp;<strong>TODO</strong>')
    106107           
    107108class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
    108109    """
Back to Top