Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Reuse the Django admin's javascript (example with prepopulated_fields.js)
<script src="{% static "admin/js/jquery.js" %}"></script>
<script src="{% static "admin/js/jquery.init.js" %}"></script>
<script src="{% static "admin/js/urlify.js" %}"></script>
<script src="{% static "admin/js/prepopulate.js" %}"></script>
<script>
// adaptation from django/contrib/admin/templates/admin/prepopulated_fields_js.html
(function($) {
var field = {
id: '#{{ form.###DESTINATION FIELD NAME###.auto_id }}',
dependency_ids: ['#{{ form.###SOURCE FIELD NAME###.auto_id }}'],
dependency_list: ['{{ form.###SOURCE FIELD NAME###.name }}'],
maxLength: {{ form.###DESTINATION FIELD NAME###.field.max_length|default_if_none:"50"|unlocalize }}
};
$('.field-{{ form.###DESTINATION FIELD NAME###.name }}').addClass('prepopulated_field');
$(field.id).data('dependency_list', field['dependency_list'])
.prepopulate(field['dependency_ids'], field.maxLength);
})(django.jQuery);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment