【问题标题】:No JSON object could be decoded with tastypie没有 JSON 对象可以用美味派解码
【发布时间】:2013-08-02 07:09:20
【问题描述】:

我正在尝试测试我的 API,但我以前从未遇到过这个问题。我收到错误消息:No JSON object could be decoded。我真的被困住了,不胜感激。我什至不确定如何调试它们。

这只发生在“POST”上

这是我的 API

from reflection.feedback.models import Feedback
from django.contrib.auth.models import User
from tastypie import fields
from tastypie.api import Api
from tastypie.authentication import Authentication
from tastypie.authorization import Authorization
from tastypie.exceptions import NotFound
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS

class DjangoAuthentication(Authentication):
    """Authenticate based upon Django session"""
    def is_authenticated(self, request, **kwargs):
        return request.user.is_authenticated()

class CommonMeta:
    authentication = DjangoAuthentication()
    authorization = Authorization()
    always_return_data = True

class UserResource(ModelResource):

    class Meta(CommonMeta):
        queryset = User.objects.all()
        resource_name = 'user'
        excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
        filtering = {
            'username': ALL,
        }

class FeedbackResource(ModelResource):

    user = fields.ForeignKey(UserResource, 'user')

    class Meta(CommonMeta):
        queryset = Feedback.objects.all()
        resource_name = 'feedback'
        excludes = ['created',]
        allowable_methods = ['post',]
        filtering = {
            'user': ALL_WITH_RELATIONS
        }

    def obj_create(self, bundle, request=None, **kwargs):
        return super(FeedbackResource, self).obj_create(bundle, request)

【问题讨论】:

    标签: django json tastypie


    【解决方案1】:

    我认为您的 Python 代码没有任何问题,如果您使用 cURL 在 Windows 上发出请求,请确保 JSON 数据用双引号引起来,并且 json 中的所有双引号用反斜杠转义。

    【讨论】:

      猜你喜欢
      • 2012-07-09
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 2012-10-30
      • 2014-01-23
      相关资源
      最近更新 更多