I'm new to django and I'm having trouble testing custom actions(e.g actions=['mark_as_read']) that are in the drop down on the app_model_changelist, it's the same dropdown with the standard "delete selected". The custom actions work in the admin view, but I just dont know how to call it in my mock request, I know I need to post data but how to say I want "mark_as_read" action to be done on the data I posted?
I want to reverse the changelist url and post the queryset so the "mark_as_read" action function will process the data I posted.
change_url = urlresolvers.reverse('admin:app_model_changelist')
response = client.post(change_url, <QuerySet>)
This is what I do:
A few notes on that, comparing to the accepted answer:
values_list
instead of list comprehension to obtain the ids.follow=True
because it is expected that a successfull post will lead to a redirectJust pass the parameter
action
with the action name.Checked items are passed as
_selected_action
parameter. So code will be like this:Here is how you do it with login and everything, a complete test case:
Just modify to use your model and custom action and run your test.
UPDATE: If you get a 302, you may need to use
follow=True
inself.client.post()
.