Ticket #3737: auto_populate_fields_from_filters.patch

File auto_populate_fields_from_filters.patch, 1.9 KB (added by lakin@…, 18 years ago)

Auto populate fields in the add form based on filter query string.

  • contrib/admin/templates/admin/change_list.html

     
    99<div id="content-main">
    1010{% block object-tools %}
    1111{% if has_add_permission %}
    12 <ul class="object-tools"><li><a href="add/{% if is_popup %}?_popup=1{% endif %}" class="addlink">{% blocktrans with cl.opts.verbose_name|escape as name %}Add {{ name }}{% endblocktrans %}</a></li></ul>
     12<ul class="object-tools"><li><a href="add/{{cl.get_add_query_string}}{% if is_popup %}&_popup=1{% endif %}" class="addlink">{% blocktrans with cl.opts.verbose_name|escape as name %}Add {{ name }}{% endblocktrans %}</a></li></ul>
    1313{% endif %}
    1414{% endblock %}
    1515<div class="module{% if cl.has_filters %} filtered{% endif %}" id="changelist">
  • contrib/admin/views/main.py

     
    599599            elif v is not None:
    600600                p[k] = v
    601601        return '?' + '&amp;'.join(['%s=%s' % (k, v) for k, v in p.items()]).replace(' ', '%20')
    602 
     602   
     603    def get_add_query_string(self):
     604        from django.db.models import query
     605 
     606        p = self.params.copy()
     607       
     608        new_query_dict = {}
     609        for k in p.keys():
     610            path = k.split(query.LOOKUP_SEPARATOR)
     611            lookup_type = path.pop()
     612            if len(path) == 0 or lookup_type not in query.QUERY_TERMS:
     613                path.append(lookup_type)
     614            new_query_dict["_".join(path)] = p[k]
     615        return '?' + '&amp;'.join(['%s=%s' % (k, v) for k, v in new_query_dict.items()]).replace(' ', '%20')
     616   
    603617    def get_results(self, request):
    604618        paginator = ObjectPaginator(self.query_set, self.lookup_opts.admin.list_per_page)
    605619
Back to Top