Django的tastypie。默认情况下,将JSON输出到浏览器 [英] Django-tastypie. Output in JSON to the browser by default

查看:303
本文介绍了Django的tastypie。默认情况下,将JSON输出到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到对不起,尚未实现。请将?format = json附加到
您的URL。我需要总是追加字符串?format = json。默认情况下,我可以在JSON中输出一个
的输出吗?



Regards,
Vitaliy

解决方案

tastypie cookbook ,为了更改默认格式,您需要重写ModelResource上的determin_format()方法:

  class MyResource(ModelResource):
....
def resolve_format(self,request):
return'application / json'

以上链接显示了确定输出格式的替代方法。



此外,



修改



GregM的答案可能是(我没有测试过)最新版本的TastyPie,根据文档将以下内容放在您的 settings.py 中将会将序列化输出限制为json。

  TASTYPIE_DEFAULT_FORMATS = ['json'] 


I see 'Sorry, not implemented yet. Please append "?format=json" to your URL.'. I need always append string "?format=json". Can I make a output in JSON by default?

Regards, Vitaliy

解决方案

From the tastypie cookbook, in order to change the default format, you need to override the determine_format() method on your ModelResource:

class MyResource(ModelResource):
    ....
    def determine_format(self, request):
        return 'application/json'

The above link demonstrates alternative methods of determining output format.

Also, I don't think a valid answer is essentially "You don't need this".

Edit

It appears GregM's answer is probably (I haven't tested it) the most correct with the new version of TastyPie, as per documentation putting the following in your settings.py will restrict the serialization output to json.

 TASTYPIE_DEFAULT_FORMATS = ['json']

这篇关于Django的tastypie。默认情况下,将JSON输出到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆