5

How do you use Django's unittesting Client to fill in inline forms?

In my test, I've tried:

response = client.get('/admin/myapp/prospect/add/')
initial = response.context['adminform'].form.initial
initial['name'] = 'Jon Doe'
response = client.post('/admin/myapp/prospect/add/', initial, follow=True)

but this throws a "ManagementForm data is missing" error because my ModelAdmin has some inline forms, and the form.initial object doesn't appear to include the boilerplate fields for these inlines, like *-INITIAL_FORMS, *-MAX_NUM_FORMS, and *-TOTAL_FORMS.

Is there a way around this, or does Django's unittest framework not support testing inline forms?

Cerin
  • 60,957
  • 96
  • 316
  • 522

1 Answers1

0
  1. Modify your application's admin inlines to include extra fields. This way you will get some more of them:

    from myapp.admin import MyModelAdmin   
    
    def test_my_test_function():
    
        MyModelAdmin.inlines[0].extra = 5
    
        # Rest of test procedure follows
    
  2. Use selenium. This way you can simulate the whole browser experience, at the cost of test running longer. You will need to click the "add new inline" link, which I was not able to click in a nice way and ended up using a very long xpath instead.

  3. Probably the best way to simulate adding new inlines in Python client could be cloning fields in your Django web test client of choice, just as Django does it client-side and then allows them server-side. I guess this would require writing some new code at the moment of writing this answer.

dotz
  • 884
  • 1
  • 8
  • 17