diff options
| author | Brian Rosner <brosner@gmail.com> | 2008-08-15 17:38:39 +0000 |
|---|---|---|
| committer | Brian Rosner <brosner@gmail.com> | 2008-08-15 17:38:39 +0000 |
| commit | d7467a0bfcc540821193c6cadd6d59a0eac1141e (patch) | |
| tree | 1c19ce0bb8fe5506cf7c482d0e64209e9ba1d156 | |
| parent | f586c0b039170f0351832d8c57cce3bf7f4772fb (diff) | |
Fixed #957 -- prepopulated_fields now works correctly on inlines.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8385 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/contrib/admin/options.py | 10 | ||||
| -rw-r--r-- | django/contrib/admin/templates/admin/change_form.html | 15 | ||||
| -rw-r--r-- | django/contrib/admin/templates/admin/prepopulated_fields_js.html | 11 | ||||
| -rw-r--r-- | django/contrib/admin/templatetags/admin_modify.py | 16 |
4 files changed, 37 insertions, 15 deletions
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 %} -<script type="text/javascript"> -{% for field in adminform.prepopulated_fields %} - document.getElementById("{{ field.field.auto_id }}").onchange = function() { this._changed = true; }; - {% for dependency in field.dependencies %} - document.getElementById("{{ dependency.auto_id }}").onkeyup = function() { - var e = document.getElementById("{{ field.field.auto_id }}"); - if (!e._changed) { e.value = URLify({% for innerdep in field.dependencies %}document.getElementById("{{ innerdep.auto_id }}").value{% if not forloop.last %} + ' ' + {% endif %}{% endfor %}, {{ field.field.field.max_length }}); } - } - {% endfor %} -{% endfor %} -</script> -{% endif %} +{% prepopulated_fields_js %} </div> </form></div> 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 @@ +<script type="text/javascript"> +{% for field in prepopulated_fields %} + document.getElementById("{{ field.field.auto_id }}").onchange = function() { this._changed = true; }; + {% for dependency in field.dependencies %} + document.getElementById("{{ dependency.auto_id }}").onkeyup = function() { + var e = document.getElementById("{{ field.field.auto_id }}"); + if (!e._changed) { e.value = URLify({% for innerdep in field.dependencies %}document.getElementById("{{ innerdep.auto_id }}").value{% if not forloop.last %} + ' ' + {% endif %}{% endfor %}, {{ field.field.field.max_length }}); } + } + {% endfor %} +{% endfor %} +</script>
\ 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'] |
