Search code examples
djangodjango-testing

RequestFactory & reverse with pk (must be called with either an object pk or a slug)


So I have this code:

request = self.factory.get(reverse('portal-edit-automation', args=(self.rule.id,)))
response = EditAutomation.as_view()(request)

And if I set a pdb break point right before it and do:

(Pdb) reverse('portal-edit-automation', args=(self.rule.id,))
u'/portal/automations/edit/1/'

I get the expected response.

So why am I getting this when running the test suite?

AttributeError: Generic detail view EditAutomation must be called with either an object pk or a slug.

Solution

  • You have to call the view with the request and the primary key, for example:

    EditAutomation.as_view()(request, pk=self.rule.id)