Making prepopulate_from work with ForeignKeys and other sorts of choice fields

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<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 }}").{% if dependency.field.choices %}onchange{% else %}onkeyup{% endif %} = function() {
    var e = document.getElementById("{{ field.field.auto_id }}");
    if (!e._changed) {
        e.value = URLify({% for innerdep in field.dependencies %}{% if innerdep.field.choices %}document.getElementById("{{ innerdep.auto_id }}").options[document.getElementById("{{ innerdep.auto_id }}").selectedIndex].text{% else %}document.getElementById("{{ innerdep.auto_id }}").value{% endif %}{% if not forloop.last %} + ' ' + {% endif %}{% endfor %}, {{ field.field.field.max_length|default_if_none:"50" }});
    }
}
    {% endfor %}
{% endfor %}
</script>

More like this

  1. Auto slug field by GaretJax 5 years, 1 month ago
  2. Replace model select widget in admin with a readonly link to the related object by ekellner 4 years, 9 months ago
  3. ForeignKey filterspec by luc_j 2 years, 9 months ago
  4. Hyperlink read-only ForeignKey objects in admin to their change pages by strangefeatures 1 year, 4 months ago
  5. Changelist filter by ForeignKey by overclocked 2 years, 7 months ago

Comments

(Forgotten your password?)