Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit d7467a0bfcc540821193c6cadd6d59a0eac1141e 1 parent f586c0b
@brosner brosner authored
View
10 django/contrib/admin/options.py
@@ -822,6 +822,14 @@ def __init__(self, parent_model, admin_site):
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 @@ def fields(self):
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
View
15 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>
View
11 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>
View
16 django/contrib/admin/templatetags/admin_modify.py
@@ -2,6 +2,22 @@
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']
Please sign in to comment.
Something went wrong with that request. Please try again.