2014-07-21 2 views
0

나는 tastypie를 사용하고있다. 내 서버에 데이터를 게시 할 때 나는 점점 오전 :나는 tastypie에서 400 BAD REQUEST를 얻고있다

내 컬 명령은 다음과 같습니다

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{{"shop" : "/api/shop/1/","transactions" : [{"item" : "/api/item/53/","note" : "Normal"}]}' 'http://localhost:5000/api/order/' 

응답은 다음과 같습니다

HTTP/1.0 400 BAD REQUEST 
Date: Mon, 21 Jul 2014 13:41:52 GMT 
Server: WSGIServer/0.1 Python/2.7.5 
Access-Control-Allow-Headers: Origin,Content-Type,Accept 
Content-Language: tr 
Vary: Accept-Language, Cookie 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE 
Content-Type: text/html; charset=utf 

테이블 생성 구문 :

CREATE TABLE `t_order` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `table` varchar(100) NOT NULL, 
    `shop_id` int(11) NOT NULL, 
    `date_modified` datetime NOT NULL, 
    `status` int(11) DEFAULT '0', 
    `date_created` datetime NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `shop_id_refs_id_d3748fa9` (`shop_id`), 
    CONSTRAINT `shop_id_refs_id_d3748fa9` FOREIGN KEY (`shop_id`) REFERENCES `t_shop` (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=231 DEFAULT CHARSET=utf8; 

CREATE TABLE `t_transaction` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `order_id` int(11) NOT NULL, 
    `note` varchar(500) NOT NULL, 
    `item_id` int(11) NOT NULL, 
    `closed` tinyint(1) NOT NULL, 
    `date_modified` datetime NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `item_id_refs_id_348f1ee9` (`item_id`), 
    KEY `order_id_refs_id_d9a0bcc6` (`order_id`), 
    CONSTRAINT `item_id_refs_id_348f1ee9` FOREIGN KEY (`item_id`) REFERENCES `t_item` (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=71 DEFAULT CHARSET=utf8; 

내 모델 :

class Order(models.Model): 
STATUSES = (
    (0, 'OPEN'), 
    (1, 'CLOSED'), 
    (2, 'CANCELLED'), 
) 
table = CharField(max_length=100) 
shop = ForeignKey(Shop) 
date_modified = DateTimeField(auto_now=True) 
date_created = DateTimeField(auto_now_add=True) 
status = IntegerField(choices=STATUSES) 
class Meta: 
     db_table = 't_order' 

class Transaction(models.Model): 
order = ForeignKey('Order') 
note = CharField(max_length=500) 
item = ForeignKey(Item) 
closed = BooleanField(default=False) 
date_modified = DateTimeField(auto_now=True) 

class Meta: 
     db_table = 't_transaction' 

무엇이 문제입니까?

+0

django 코드 (uwsgi?)를 어떻게 실행하며 로그를 생성합니까? – RickyA

답변

1

JSON이 유효하지 않습니다. 컬 데이터 here의 유효성을 확인하십시오. 400 (나쁜 요청) 상태는 그것에 대한 단서를 제공합니다. 해당 예외를 처리 할 수있는 serializer를 만들 수 있는지 확인하십시오.

from tastypie.exceptions import BadRequest 

class VerboseSerializer(Serializer): 
    """ 
    Gives message when loading JSON fails. 
    """ 
    # Tastypie>=0.9.6,<=0.11.0 
    def from_json(self, content): 
     """ 
     Override method of `Serializer.from_json`. Adds exception message when loading JSON fails. 
     """ 
     try: 
      return json.loads(content) 
     except ValueError as e: 
      raise BadRequest(u"Incorrect JSON format: Reason: \"{}\" (See www.json.org for more info.)".format(e.message)) 

class MyResource(BaseModelResource): 
    class Meta: 
     serializer = VerboseSerializer(formats=['json']) 
관련 문제