如何在 Django 测试客户端中创建 patch/update(即更新对象)请求?

How to create a patch/update (i.e. update an object) request in Django test client?

我正在使用 Django 1.5。和 django-tastypie

我正在测试一个用例,首先我必须创建一个对象,然后通过 rest api 更新该对象。 例如

class FooTest(TestCase):
    fixtures = ['df_fixtures1.json']

    def setUp(self):
        print "SETTING UP?"
    def tearDown(self):
        print "Tear Down"
    def test_foo_delete(self):
          member1 = Client()
          member1.login(username='member1',password=test_password)
          response = member1.post('/fooapi/api/foo/?format=json', json_data, content_type="application/json") #**This creates the foo object**
          META =  {'X-HTTP-Method-Override':'PATCH'}
          response123 = member1.put(response['location'],
                        '{"isActive":0}', 
                       content_type="application/json", META = META    
                       ) **#This gives a 501**

第二次请求501错误。在服务器端有一个 def obj_update 即处理 update/patch 请求的方法。 使用 Django 客户端更新对象的最佳方法是什么 api.

由于 "PATCH" 方法从 django 1.6 开始可用,因此可以用作 Django 1.5 上的 hack

response123 = member1.put(response['location'],
                        data = data, 
                        content_type="application/json",
                        **{'REQUEST_METHOD':'PATCH'}
                       )

这会将方法从 put 更改为 PATCH。希望这可以帮助别人。