testing admin.ModelAdmin in django

前端 未结 4 1180
深忆病人
深忆病人 2021-01-31 08:03

I am trying to find out the best way for testing admin.ModelAdmin in admin.py. Specifically I am overriding the save_model() function whic

4条回答
  •  醉酒成梦
    2021-01-31 08:35

    You can specify custom modelform for modeladmin then simply test this modelform ;)

    https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form

    forms

    class SomeModelForm(forms.ModelForm):
        class Meta:
            model = SomeModel
    

    admin

    class SomeModelAdmin(admin.ModelAdmin):
        form = SomeModelForm
    
    
    admin.site.register(SomeModel, SomeModelAdmin)
    

    tests

    class TestSomeModel(TestCase):
        def test_form(self):
            form = SomeModelForm(**kwargs)
            self.assertTrue(form.is_valid())
            # ...
    

提交回复
热议问题