django tastypie manytomany field POST json error

本小妞迷上赌 提交于 2019-12-10 12:08:59

问题


Here are my resources:

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()  

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/

This is what i get:

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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/9413176/django-tastypie-manytomany-field-post-json-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!