Django tastypie POST 400错误请求

时间:2016-12-21 14:34:30

标签: javascript jquery python django tastypie

Django 1.7和django-tastypie 0.13.3存在以下问题,从Django 1.6和django-tastypie 0.9.14升级。

urls.py

from relationships.api import RelationResource

v1_api = Api(api_name='v1')
v1_api.register(RelationResource())

在模板中调用此代码:

var Relationships = Relationships || {};

$.ajaxPrefilter(function(options, originalOptions, jqXHR){
    if(options["type"].toLowerCase() != "get") {
        options["contentType"] = "application/json";
        options["data"] = JSON.stringify(originalOptions["data"]);
    }  
});  

Relationships.Relation = can.Model({
    attributes: {
        id: 'to_int'
    },
    convert: {
        to_int: function(raw) {
            return parseInt(raw);
        }
    }, 
    models: function(data){
        return data.objects;
    },
    model: function( attributes ) {
        return new this(attributes);
    },

    findAll: "GET /api/v1/relationships/",
    findOne: "GET /api/v1/relationships/{id}/",
    create: "POST /api/v1/relationships/",
    update: "PUT /api/v1/relationships/{id}/",
    destroy: "DELETE /api/v1/relationships/{id}/"
},{ 
});

Relationships.Button = can.Control({
    init: function(element, options){
    },
    'click': function(el, ev){
        var following = Boolean(el.data('following'));
        var from_user = el.data('from_user');
        var to_user = el.data('to_user');

        var url = '/api/v1/relationships/';
        var from_user_uri = '/api/v1/users/' + from_user + '/';
        var to_user_uri = '/api/v1/users/' + to_user + '/';

        var self = this;
        if(following) {
            Relationships.Relation.findAll({
                'from_user': from_user, 'to_user': to_user}, function(items){
                    if(items.length == 1) {
                        var item = new Relationships.Relation(items[0]);
                        item.destroy(function(){
                        });
                    }
            });
        } else {
            var item = new Relationships.Relation({'to_user': to_user_uri});
            item.save(function(){
            });
        }
    }
});

这会出错: POST http://0.0.0.0:8000/api/v1/relationships/ 400(BAD REQUEST)

Firebug输出帖子标签

  

to_user“/ api / v1 / users / 6 /”

响应标签

  

{“relationships”:{“to_user”:[“选择一个有效的选择。那个选择是   不是可用的选择之一。“]}}

此代码适用于以前版本的django和tastypie。知道我错过了什么吗?

0 个答案:

没有答案