我正在使用django 1.7和Tastypie 0.12.0每当我尝试POST到django-tastypie时,都没有创建对象,并且我在此响应时出现500错误:
{"error_message": "add() argument after * must be a sequence, not Comment", "traceback": "Traceback (most recent call last):
File \"/Users/yapster2/Desktop/backend/FivesDjangoRepo/fives/lib/python2.7/site-packages/tastypie/resources.py\", line 201, in wrapper
response = callback(request, *args, **kwargs)
File \"/Users/yapster2/Desktop/backend/FivesDjangoRepo/fives/lib/python2.7/site-packages/tastypie/resources.py\", line 432, in dispatch_list
return self.dispatch('list', request, **kwargs)
File \"/Users/yapster2/Desktop/backend/FivesDjangoRepo/fives/lib/python2.7/site-packages/tastypie/resources.py\", line 464, in dispatch
response = method(request, **kwargs)
File \"/Users/yapster2/Desktop/backend/FivesDjangoRepo/fives/lib/python2.7/site-packages/tastypie/resources.py\", line 1340, in post_list
updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))
File \"/Users/yapster2/Desktop/backend/FivesDjangoRepo/fives/lib/python2.7/site-packages/tastypie/resources.py\", line 2104, in obj_create
return self.save(bundle)
File \"/Users/yapster2/Desktop/backend/FivesDjangoRepo/fives/lib/python2.7/site-packages/tastypie/resources.py\", line 2247, in save
self.save_related(bundle)
File \"/Users/yapster2/Desktop/backend/FivesDjangoRepo/fives/lib/python2.7/site-packages/tastypie/resources.py\", line 2298, in save_related
setattr(related_obj, field_object.related_name, bundle.obj)
File \"/Users/yapster2/Desktop/backend/FivesDjangoRepo/fives/lib/python2.7/site-packages/django/db/models/fields/related.py\", line 783, in __set__
manager.add(*value)
TypeError: add() argument after * must be a sequence, not Comment"}
我不想让帖子混乱,所以如果你需要查看特定的代码,请告诉我。
这是我的模特:
class Comment(models.Model):
post = models.ForeignKey(NewsFeedPost, related_name='comments', null=True, blank=True)
comment_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='comments', null=True, blank=True)
timestamp = models.DateTimeField(null=True, blank=True)
body = models.CharField(max_length=1000, null=True, blank=True)
hashtags = models.ManyToManyField(Hashtag, related_name='comments', null=True, blank=True)
我的资源:
class CommentResource(ModelResource):
post = fields.ForeignKey('newsfeed.api.NewsFeedPostResource', 'post', related_name='comments',
null=True, blank=True, full=True)
comment_by = fields.ForeignKey('login.api.UserResource', 'comment_by', related_name='comments',
null=True, blank=True, full=True)
hashtags = fields.ManyToManyField('newsfeed.api.HashtagResource', 'hashtag_set',
related_name='comments', null=True, blank=True, full=True)
class Meta:
queryset = Comment.objects.all()
resource_name = 'comments'
authorization = Authorization()
filtering = {
"post": ALL_WITH_RELATIONS
}
allowed_methods = ['get', 'post', 'put']