Models.py
class Result(models.Model):
host = models.CharField(max_length=200)
type = models.CharField(max_length=1, choices=TYPE_CHOICES)
active = models.BooleanField(default=False)
current = models.FloatField()
value = models.FloatField()
notify = models.BooleanField(default=False)
info = models.TextField()
test = models.ForeignKey('test', related_name='result')
group = models.ForeignKey(Group, blank=True, null=True, default=None, related_name='result')
timestamp = models.DateTimeField()
created = models.DateTimeField(auto_now=True)
我的资源:
class ResultResource(ModelResource):
test = fields.ForeignKey(TestResource, 'test', full=True, related_name='result')
group = fields.ForeignKey(GroupResource, 'group', full=True, related_name='result')
class Meta:
queryset = Result.objects.all()
list_allowed_methods = ['get', 'post']
resource_name = 'result'
fields = ['host', 'type', 'current', 'value', 'notify', 'info', 'timestamp']
include_resource_uri = False
excludes = ['id', 'group']
authentication = Authentication()
authorization = Authorization()
我想通过帖子请求创建新资源。
这是我的JSON:
{
"current": 26.74,
"host": "host 1",
"info": "info text",
"notify": true,
"test": "/api/v1/test/1/",
"timestamp": "2010-11-10T03:07:43",
"type": "w",
"value": 10,
"group" : "/api/v1/group/1/"
}
测试和小组存在。
但是我得到了这个错误:“error_message”:*之后的“add()参数必须是序列,而不是结果”。
我读到tastypie在Many2Many字段中存在一些问题,因此我将Many2Many字段更改为Foreignkey字段。但它并没有解决问题。
编辑:堆栈跟踪
File "/home/hschneider/workspace/python/IGS/env/local/lib/python2.7/site-packages/tastypie/resources.py", line 217, in wrapper
response = callback(request, *args, **kwargs)
File "/home/hschneider/workspace/python/IGS/env/local/lib/python2.7/site-packages/tastypie/resources.py", line 459, in dispatch_list
return self.dispatch('list', request, **kwargs)
File "/home/hschneider/workspace/python/IGS/env/local/lib/python2.7/site-packages/tastypie/resources.py", line 491, in dispatch
response = method(request, **kwargs)
File "/home/hschneider/workspace/python/IGS/env/local/lib/python2.7/site-packages/tastypie/resources.py", line 1357, in post_list
updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))
File "/home/hschneider/workspace/python/IGS/env/local/lib/python2.7/site-packages/tastypie/resources.py", line 2150, in obj_create
return self.save(bundle)
File "/home/hschneider/workspace/python/IGS/env/local/lib/python2.7/site-packages/tastypie/resources.py", line 2293, in save
self.save_related(bundle)
File "/home/hschneider/workspace/python/IGS/env/local/lib/python2.7/site-packages/tastypie/resources.py", line 2344, in save_related
setattr(related_obj, field_object.related_name, bundle.obj)
File "/home/hschneider/workspace/python/IGS/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 474, in __set__
manager.add(*value)
TypeError: add() argument after * must be a sequence, not Result