Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing custom admin actions in django

Tags:

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>) 
like image 435
user2106729 Avatar asked Mar 13 '15 07:03

user2106729


People also ask

How can I get admin panel in Django?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

How do I run a TestCase in Django?

Open /catalog/tests/test_models.py.TestCase , as shown: from django. test import TestCase # Create your tests here. Often you will add a test class for each model/view/form you want to test, with individual methods for testing specific functionality.


1 Answers

Just pass the parameter action with the action name.

response = client.post(change_url, {'action': 'mark_as_read', ...}) 

Checked items are passed as _selected_action parameter. So code will be like this:

fixtures = [MyModel.objects.create(read=False),             MyModel.objects.create(read=True)] should_be_untouched = MyModel.objects.create(read=False)  #note the unicode() call below data = {'action': 'mark_as_read',         '_selected_action': [unicode(f.pk) for f in fixtures]} response = client.post(change_url, data) 
like image 192
catavaran Avatar answered Oct 13 '22 01:10

catavaran