django tastypie manytomany field POST json错误

时间:2012-02-23 12:34:24

标签: django json http-post tastypie

以下是我的资源:

class CourseResource(ModelResource):  
    subjects = fields.ToManyField('core.api.SubjectResource', 'subjects', full=True)  
    class Meta:
        queryset = Course.objects.all()  
        resource_name = 'course'  
        authorization = Authorization()   
        validation = FormValidation(form_class=CourseForm) 

class SubjectResource(ModelResource):  
    class Meta:  
        queryset = Subject.objects.all()  
        resource_name = 'subject'  
        authorization = Authorization()  

我试图在django-tastypie系统上使用curl发布。

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"title":"title 123","description":"sdfsadfsdf","created_by":"1","created_on":"2012-02-23 03:24:56","initial-created_on":"2012-02-23 03:24:56.419838","subjects":["/api/v1/subject/1/"]}' http://127.0.0.1:8000/api/v1/course/

这就是我得到的:

HTTP/1.0 400 BAD REQUEST
Date: Thu, 23 Feb 2012 12:28:15 GMT
Server: WSGIServer/0.1 Python/2.7.2+
Content-Type: application/json; charset=utf-8

{"subjects": ["\"/api/v1/subject/1/\" is not a valid value for a primary key."]}%

我也尝试过只发送id而不是resource_uri,但这也行不通。我确信我的帖子数据在某种程度上是错误的。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我自己修正了错误。罪魁祸首是我正在使用"通过"在我的models.py中,用于ManyToMany字段连接器。通过移除使得轻松处理m2m节省。否则,我使用'通过'也应该工作但我必须创建连接器的资源并发送适当的端点。