I have customized save_model
admin i.e.
class MyModelAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
# some more question code here
obj.save()
Now, I would like to test MyModelAdmin save_model
function. I tried posting like:
class MyModelAdminSaveTestCase(TestCase):
def setUp(self):
# setup code here
def test_save_model(self):
'''Test add employee
'''
my_obj = {
'name': 'Tester',
'address': '12 test Test',
'city': 'New York',
'state': 'NY',
}
self.client.login(username=self.user, password=self.pwd)
response = self.client.post(reverse('admin:mymodel_mymodel_add'), my_obj, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(MyModel.objects.count(), 1)
However, test fails:
self.assertEqual(MyModel.objects.count(), 1)
AssertionError: 0 != 1
admin:mymodel_mymodel_add
– Papottofollow=False
and expect 302 status code. – Vitricsresponse.content
in the test - that will show you whether there are any errors on the page. If the response is too long, you could try to get the form errors directly with something likeresponse.context[' 'adminform'].form.errors
. – Ingrid