diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 2e6bcfcfcd..5984dd82f3 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -822,6 +822,14 @@ class InlineModelAdmin(BaseModelAdmin): self.verbose_name = self.model._meta.verbose_name if self.verbose_name_plural is None: self.verbose_name_plural = self.model._meta.verbose_name_plural + + def _media(self): + from django.conf import settings + js = [] + if self.prepopulated_fields: + js.append('js/urlify.js') + return forms.Media(js=['%s%s' % (settings.ADMIN_MEDIA_PREFIX, url) for url in js]) + media = property(_media) def get_formset(self, request, obj=None): """Returns a BaseInlineFormSet class for use in admin add/change views.""" @@ -866,7 +874,7 @@ class InlineAdminFormSet(object): yield self.formset.form.base_fields[field_name] def _media(self): - media = self.formset.media + media = self.opts.media + self.formset.media for fs in self: media = media + fs.media return media diff --git a/django/contrib/admin/templates/admin/change_form.html b/django/contrib/admin/templates/admin/change_form.html index e8df6b99f5..f5809fb143 100644 --- a/django/contrib/admin/templates/admin/change_form.html +++ b/django/contrib/admin/templates/admin/change_form.html @@ -58,20 +58,7 @@ {% endif %} {# JavaScript for prepopulated fields #} - -{% if add %} - -{% endif %} +{% prepopulated_fields_js %} diff --git a/django/contrib/admin/templates/admin/prepopulated_fields_js.html b/django/contrib/admin/templates/admin/prepopulated_fields_js.html new file mode 100644 index 0000000000..9e78c34b35 --- /dev/null +++ b/django/contrib/admin/templates/admin/prepopulated_fields_js.html @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/django/contrib/admin/templatetags/admin_modify.py b/django/contrib/admin/templatetags/admin_modify.py index 25d2d6774a..62c421536c 100644 --- a/django/contrib/admin/templatetags/admin_modify.py +++ b/django/contrib/admin/templatetags/admin_modify.py @@ -2,6 +2,22 @@ from django import template register = template.Library() +def prepopulated_fields_js(context): + """ + Creates a list of prepopulated_fields that should render Javascript for + the prepopulated fields for both the admin form and inlines. + """ + prepopulated_fields = [] + if context["add"]: + prepopulated_fields.extend(context["adminform"].prepopulated_fields) + for inline_admin_formset in context['inline_admin_formsets']: + for inline_admin_form in inline_admin_formset: + if inline_admin_form.original is None: + prepopulated_fields.extend(inline_admin_form.prepopulated_fields) + context.update({"prepopulated_fields": prepopulated_fields}) + return context +prepopulated_fields_js = register.inclusion_tag('admin/prepopulated_fields_js.html', takes_context=True)(prepopulated_fields_js) + def submit_row(context): opts = context['opts'] change = context['change']