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
This commit is contained in:
Brian Rosner 2008-08-15 17:38:39 +00:00
parent f586c0b039
commit d7467a0bfc
4 changed files with 37 additions and 15 deletions

View File

@ -823,6 +823,14 @@ class InlineModelAdmin(BaseModelAdmin):
if self.verbose_name_plural is None: if self.verbose_name_plural is None:
self.verbose_name_plural = self.model._meta.verbose_name_plural 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): def get_formset(self, request, obj=None):
"""Returns a BaseInlineFormSet class for use in admin add/change views.""" """Returns a BaseInlineFormSet class for use in admin add/change views."""
if self.declared_fieldsets: if self.declared_fieldsets:
@ -866,7 +874,7 @@ class InlineAdminFormSet(object):
yield self.formset.form.base_fields[field_name] yield self.formset.form.base_fields[field_name]
def _media(self): def _media(self):
media = self.formset.media media = self.opts.media + self.formset.media
for fs in self: for fs in self:
media = media + fs.media media = media + fs.media
return media return media

View File

@ -58,20 +58,7 @@
{% endif %} {% endif %}
{# JavaScript for prepopulated fields #} {# JavaScript for prepopulated fields #}
{% prepopulated_fields_js %}
{% 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 %}
</div> </div>
</form></div> </form></div>

View File

@ -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 File

@ -2,6 +2,22 @@ from django import template
register = template.Library() 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): def submit_row(context):
opts = context['opts'] opts = context['opts']
change = context['change'] change = context['change']