Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Brian Rosner authored August 15, 2008
10  django/contrib/admin/options.py
@@ -822,6 +822,14 @@ def __init__(self, parent_model, admin_site):
822 822
             self.verbose_name = self.model._meta.verbose_name
823 823
         if self.verbose_name_plural is None:
824 824
             self.verbose_name_plural = self.model._meta.verbose_name_plural
  825
+    
  826
+    def _media(self):
  827
+        from django.conf import settings
  828
+        js = []
  829
+        if self.prepopulated_fields:
  830
+            js.append('js/urlify.js')
  831
+        return forms.Media(js=['%s%s' % (settings.ADMIN_MEDIA_PREFIX, url) for url in js])
  832
+    media = property(_media)
825 833
 
826 834
     def get_formset(self, request, obj=None):
827 835
         """Returns a BaseInlineFormSet class for use in admin add/change views."""
@@ -866,7 +874,7 @@ def fields(self):
866 874
             yield self.formset.form.base_fields[field_name]
867 875
 
868 876
     def _media(self):
869  
-        media = self.formset.media
  877
+        media = self.opts.media + self.formset.media
870 878
         for fs in self:
871 879
             media = media + fs.media
872 880
         return media
15  django/contrib/admin/templates/admin/change_form.html
@@ -58,20 +58,7 @@
58 58
 {% endif %}
59 59
 
60 60
 {# JavaScript for prepopulated fields #}
61  
-
62  
-{% if add %}
63  
-<script type="text/javascript">
64  
-{% for field in adminform.prepopulated_fields %}
65  
-    document.getElementById("{{ field.field.auto_id }}").onchange = function() { this._changed = true; };
66  
-    {% for dependency in field.dependencies %}
67  
-    document.getElementById("{{ dependency.auto_id }}").onkeyup = function() {
68  
-        var e = document.getElementById("{{ field.field.auto_id }}");
69  
-        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 }}); }
70  
-    }
71  
-    {% endfor %}
72  
-{% endfor %}
73  
-</script>
74  
-{% endif %}
  61
+{% prepopulated_fields_js %}
75 62
 
76 63
 </div>
77 64
 </form></div>
11  django/contrib/admin/templates/admin/prepopulated_fields_js.html
... ...
@@ -0,0 +1,11 @@
  1
+<script type="text/javascript">
  2
+{% for field in prepopulated_fields %}
  3
+    document.getElementById("{{ field.field.auto_id }}").onchange = function() { this._changed = true; };
  4
+    {% for dependency in field.dependencies %}
  5
+    document.getElementById("{{ dependency.auto_id }}").onkeyup = function() {
  6
+        var e = document.getElementById("{{ field.field.auto_id }}");
  7
+        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 }}); }
  8
+    }
  9
+    {% endfor %}
  10
+{% endfor %}
  11
+</script>
16  django/contrib/admin/templatetags/admin_modify.py
@@ -2,6 +2,22 @@
2 2
 
3 3
 register = template.Library()
4 4
 
  5
+def prepopulated_fields_js(context):
  6
+    """
  7
+    Creates a list of prepopulated_fields that should render Javascript for
  8
+    the prepopulated fields for both the admin form and inlines.
  9
+    """
  10
+    prepopulated_fields = []
  11
+    if context["add"]:
  12
+        prepopulated_fields.extend(context["adminform"].prepopulated_fields)
  13
+    for inline_admin_formset in context['inline_admin_formsets']:
  14
+        for inline_admin_form in inline_admin_formset:
  15
+            if inline_admin_form.original is None:
  16
+                prepopulated_fields.extend(inline_admin_form.prepopulated_fields)
  17
+    context.update({"prepopulated_fields": prepopulated_fields})
  18
+    return context
  19
+prepopulated_fields_js = register.inclusion_tag('admin/prepopulated_fields_js.html', takes_context=True)(prepopulated_fields_js)
  20
+
5 21
 def submit_row(context):
6 22
     opts = context['opts']
7 23
     change = context['change']

0 notes on commit d7467a0

Please sign in to comment.
Something went wrong with that request. Please try again.