python – AttributeError: cant set attribute

python – AttributeError: cant set attribute

This answer doesnt address the specifics of this question, but explains the underlying issue.
This specific exception AttributeError: cant set attribute is raised (see source) when the attribute youre attempting to change is actually a property that doesnt have a setter. If you have access to the librarys code, adding a setter would solve the problem.

EDIT: updated source link to new location in the code.

It looks like you dont use self.template in Response class. Try like this:

class Response(HttpResponse):
    def __init__(self, template=, calling_context= status=None):
        HttpResponse.__init__(self, get_template(template).render(calling_context), status)

python – AttributeError: cant set attribute

I took a look to django source code Ive no idea where template or templates attribute come from in HttpResponse. But I can propose to you to change your test approach and migrate to mock framework. You can rewrite your test like:

@patch(qualified_path_of_response_module.response.Response, spec=Response)
def test_should_list_all_users_for_that_specific_sales_office(self,mock_resp):
    user_company = CompanyFactory.create()
    request = self.mock_request(user_company)
    #some other stuff

    #calling the view
    response = show(request,
    context = mock_resp.call_args[0][2]
    self.assertIn(user, context[sales_office_users])
    self.assertNotIn(user2, context[sales_office_users])

@patch decorator replace your Response() class by a MagicMock() and pass it to your test method as mock_resp variable. You can also use patch as context manager by with construct but decorators are the cleaner way to do it. I dont know if Response is just a stub class for testing but in that case you can patch directly HttpResponce, but it depends from your code.

You can find details about call_args here. Maybe you need to use spec attribute because django make some type checking… but try with and without it (Im not a django expert). Explore mock framework: itll give to you lot of powerful tools to make simple tests.

Leave a Reply

Your email address will not be published.