0

我正在尝试测试我的 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)
4

1 回答 1

0

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

于 2013-08-02T07:14:32.730 回答