1
0
mirror of https://github.com/django/django.git synced 2025-10-31 01:25:32 +00:00

Fixed #7923 -- Added links to objects displayed by ModelAdmin.raw_id_fields.

This commit is contained in:
Hugo Osvaldo Barrera
2016-01-27 21:43:04 -03:00
committed by Tim Graham
parent c79852acee
commit 8bf8d0e0ec
4 changed files with 31 additions and 8 deletions

View File

@@ -11,11 +11,10 @@ from django.forms.utils import flatatt
from django.forms.widgets import RadioFieldRenderer
from django.template.loader import render_to_string
from django.urls import reverse
from django.urls.exceptions import NoReverseMatch
from django.utils import six
from django.utils.encoding import force_text
from django.utils.html import (
escape, format_html, format_html_join, smart_urlquote,
)
from django.utils.html import format_html, format_html_join, smart_urlquote
from django.utils.safestring import mark_safe
from django.utils.text import Truncator
from django.utils.translation import ugettext as _
@@ -194,10 +193,27 @@ class ForeignKeyRawIdWidget(forms.TextInput):
key = self.rel.get_related_field().name
try:
obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
return '&nbsp;<strong>%s</strong>' % escape(Truncator(obj).words(14, truncate='...'))
except (ValueError, self.rel.model.DoesNotExist):
return ''
label = '&nbsp;<strong>{}</strong>'
text = Truncator(obj).words(14, truncate='...')
try:
change_url = reverse(
'%s:%s_%s_change' % (
self.admin_site.name,
obj._meta.app_label,
obj._meta.object_name.lower(),
),
args=(obj.pk,)
)
except NoReverseMatch:
pass # Admin not registered for target model.
else:
text = format_html('<a href="{}">{}</a>', change_url, text)
return format_html(label, text)
class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
"""