Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set request.body in django Request factory post request?

trying to set request.body in python unit test as

        self.factory = RequestFactory()
        self.request = self.factory.get(reverse('get_associations'))
        self.request.user = self.auth_user
        self.request.body = json.dumps(self.post_data)

but it gives error:

self.request.body = json.dumps(self.post_data)
AttributeError: can't set attribute

can anybody help me to fix this?

like image 986
Sony Khan Avatar asked Jun 13 '16 21:06

Sony Khan


1 Answers

Based on this answer, try this:

self.request = self.factory.get(reverse('get_associations'),
                                data=json.dumps(self.post_data),
                                content_type='application/json')
like image 138
Alex Hall Avatar answered Sep 20 '22 13:09

Alex Hall