我正在尝试使用django中的tastypie进行POST请求。
我的资源如下所示:
class TestResource(ModelResource):
class Meta:
queryset = Test.objects.all()
resource_name = 'test'
serializer = Serializer(formats=['json','xml'])
always_return_data = True
detail_allowed_methods = ['get', 'post', 'put', 'delete']
我发布的数据如下:
import sys
import requests
DATA = {'field1':'posting data', 'field2':'123', 'field3':330303,}
def post(data):
url = 'http://127.0.0.1:8000/api/test/'
logging = {'verbose':sys.stderr}
response = requests.post(url,data=data,config=logging)
print "RESPONSE STATUS", response.status_code
print "RESPONSE HEADERS", response.headers
if __name__=='__main__':
post(DATA)
我总是得到401,任何想法? 我做错了什么?
提前致谢。
答案 0 :(得分:6)
您需要设置
Authorization=Authorization()
tastypie所规定的默认授权是
ReadOnlyAuthorization()
导致,你猜对了,只读结果,因此会导致错误。
http://django-tastypie.readthedocs.org/en/latest/tutorial.html
请注意,不是这样做的最佳做法。还可以使用其他授权方法。