问题
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