Python Django Tastypie添加内容长度标题
我是Django开发的新手。我有一个资源和模型: 型号Python Django Tastypie添加内容长度标题,python,django,rest,httpresponse,tastypie,Python,Django,Rest,Httpresponse,Tastypie,我是Django开发的新手。我有一个资源和模型: 型号 class Player(models.Model): pseudo = models.CharField(max_length=32, unique=True) class PlayerResource(ModelResource): class Meta: queryset = Player.objects.all() resource_name = 'player' a
class Player(models.Model):
pseudo = models.CharField(max_length=32, unique=True)
class PlayerResource(ModelResource):
class Meta:
queryset = Player.objects.all()
resource_name = 'player'
authentication = BasicAuthentication()
authorization = Authorization()
serializer = Serializer(formats=['xml'])
filtering = {
'pseudo': ALL,
}
模型资源class Player(models.Model):
pseudo = models.CharField(max_length=32, unique=True)
class PlayerResource(ModelResource):
class Meta:
queryset = Player.objects.all()
resource_name = 'player'
authentication = BasicAuthentication()
authorization = Authorization()
serializer = Serializer(formats=['xml'])
filtering = {
'pseudo': ALL,
}
我请求所有玩家使用/api/v1/player/?format=xml,但似乎缺少响应标题:Content Length,这导致我的应用程序出现一些问题。如何将其添加到响应标题中
非常感谢。您可以通过在自己的资源中覆盖create\u response方法来添加内容长度标题,例如:
class MyResource(ModelResource):
class Meta:
queryset=MyModel.objects.all()
def create_response(self, ...)
# Here goes regular code that do the method from tastypie
return response_class(content=serialized, content_type=build_content_type(desired_format), Content-Length=value, **response_kwargs)
您可以通过在自己的资源中覆盖create_Response方法来添加内容长度标题,例如:
class MyResource(ModelResource):
class Meta:
queryset=MyModel.objects.all()
def create_response(self, ...)
# Here goes regular code that do the method from tastypie
return response_class(content=serialized, content_type=build_content_type(desired_format), Content-Length=value, **response_kwargs)
缺少内容长度是由于缺少中间件。
有关更多信息: 看这里: 但是您可以像这样手动添加内容长度:
def create_response(self, request, data, response_class=HttpResponse, **response_kwargs):
desired_format = self.determine_format(request)
serialized = self.serialize(request, data, desired_format)
response = response_class(content=serialized, content_type=build_content_type(desired_format), **response_kwargs)
response['Content-Length'] = len(response.content)
return response
缺少内容长度是由于缺少中间件。
有关更多信息: 看这里: 但是您可以像这样手动添加内容长度:
def create_response(self, request, data, response_class=HttpResponse, **response_kwargs):
desired_format = self.determine_format(request)
serialized = self.serialize(request, data, desired_format)
response = response_class(content=serialized, content_type=build_content_type(desired_format), **response_kwargs)
response['Content-Length'] = len(response.content)
return response