django tastypie manytomany字段POST json错误 [英] django tastypie manytomany field POST json error

查看:188
本文介绍了django tastypie manytomany字段POST json错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的资源:

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系统上使用卷曲。

I am trying to post using curl on a django-tastypie system.

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."]}%

我已经尝试发送ids而不是resource_uri,但这也不行。我确信我的帖子数据在某些方面是错误的。我如何解决这个问题?

I have tried sending just the ids instead of the resource_uri too, but that also doesn't work. I am sure my post data is wrong in some way. How do I fix this?

推荐答案

我自己修复了错误。这个罪魁祸首是我在我的models.py中为ManyToMany字段连接器使用through。去除通过使它容易照顾m2m保存。否则,我使用'通过'的东西也应该工作,但是我必须创建连接器的资源并发送相应的端点。

I fixed the error myself. The culprit was that i was using "through" in my models.py for the ManyToMany field connector. Removing through made it easy to take care of m2m saves. Else, my thing using 'through' should also work but I will have to make a resource of the connector and send the appropriate endpoints.

这篇关于django tastypie manytomany字段POST json错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆