0

I change the admin view like described in: How Do I Show Django Admin Change List View of foreign key children?

I have Customer -> Project -> Action I get all actions for a project by this method:

def related_actions(self, obj):
    from django.core import urlresolvers
    url = urlresolvers.reverse("admin:workflow_action_changelist")
    lookup = u"project__exact"
    text = obj.name
    return u"<a href='%s?%s=%d'>%s</a>" % (url, lookup, obj.pk, text)

Now i want to customize the addlink in the change_list. The admin change_list is overwritten by a template in /workflow/templates/admin/workflow/action/change_list.html But all three models use the same template. So i think there must be a better solution as harcode param in the template When i change the line

<a href="add/{% if is_popup %}?_popup=1{% endif %}" class="addlink">

to

<a href="add/?actionstatus=5" class="addlink">

the actioncategory is preselected in the new action form. But how could i override the addlink in each model.

I must get the actionstatus by the param in the projectsview:

/workflow/action/?project__exact=5

Any suggestions?

Is there something like this pseudo for use in action model ?

 def add(self)
     get param projectid from url 
     return "<a href='/workflow/action/add/%s' target='_blank'>Add Action</a>" % (parent.project id)
Community
  • 1
  • 1
surfi
  • 1,451
  • 2
  • 12
  • 25

1 Answers1

2

AFAIK, no such add.
You could customize the change_list.html, especially its the object-list-item block by extending:

{% block object-tools-items %}
  <li>
    <a href="{% url cl.opts|admin_urlname:'add' %}{% if is_popup %}?_popup=1{% else %}?{% endif %}{% if "project" in cl.params %}&amp;project={{ cl.params.project }}{% endif %}" class="addlink">
      {% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
    </a>
  </li> 
{% endblock %}

Here, cl is the ChangeList instance. cl.params is filtered request.GET. The code checks whether there is project in querystring and appends its value to the generated addlink.

You could also write JS code to update add link when changelist page finishs loading. something like

(function($){
$(function(){
var m = window.location.search.match(/[?&]project=([^&]*)/);
if (m){
    var link = $('a.addlink').attr('href');
    $('a.addlink').attr('href', link+(link.match(/\?_popup=1$/)?'&':'?')+'project='+m[1]);
}
});
})(django.jQuery);

Check this for general params manipulation.

Community
  • 1
  • 1
okm
  • 23,575
  • 5
  • 83
  • 90
  • Thanks, with this i can add the GET params for every model. It works fine. Parent ForeignKey (Selectbox) is preselected. ;-) – surfi May 09 '12 at 14:32
  • Can you give me an advice or example JS which updates the add link? I prefer the first solution. Just for interest. Thanks. – surfi May 10 '12 at 07:34