我使用backbone-tastypie来创建嵌套资源。
class ServiceResource(ModelResource):
manager = fields.ToOneField(UserResource, 'manager',full=True)
area = fields.ToOneField(AreaResource, 'area',full=True)
specification = fields.ToManyField('services.api.ServiceSpecResource','servicespec_set',full=True)
class Meta:
queryset = Service.objects.all()
resource_name = 'service'
class ServiceSpecResource(ModelResource):
service = fields.ToOneField(ServiceResource,'service')
servicespecrange = fields.ToManyField('services.api.ServiceSpecRangeResource','servicespecrange_set',full=True)
servicespecconstant = fields.ToManyField('services.api.ServiceSpecConstantResource','servicespecconstant_set',full=True)
servicespecgeneric = fields.ToManyField('services.api.ServiceSpecGenericResource','servicespecgeneric_set',full=True)
class Meta:
queryset = ServiceSpec.objects.all()
resource_name = 'servicespec'
list_allowed_methods = ['get', 'post']
detail_allowed_methods = ['get', 'put', 'delete']
authorization = DjangoAuthorization()
class ServiceSpecRangeResource(ModelResource):
servicespec = fields.ToOneField(ServiceSpecResource,'service_spec')
class Meta:
queryset = ServiceSpecRange.objects.all()
resource_name = 'servicespecrange'
class ServiceSpecConstantResource(ModelResource):
servicespec = fields.ToOneField(ServiceSpecResource,'service_spec')
class Meta:
queryset = ServiceSpecConstant.objects.all()
resource_name = 'servicespecconstant'
class ServiceSpecGenericResource(ModelResource):
servicespec = fields.ToOneField(ServiceSpecResource,'service_spec')
class Meta:
queryset = ServiceSpecGeneric.objects.all()
resource_name = 'servicespecgeneric'
为了获得ServiceResource,它工作得很好。我返回一个Service对象,其中包含所有ServiceSpecification,而ServiceSpecification又包含所有ServiceSpecRange / constants等。
将整个嵌套资源放回服务器也适用于现有数据。
但是,每当我尝试一次发布一个完整的嵌套资源时(附带新服务规范的新服务),我得到:
int()参数必须是字符串或数字,而不是'list'
之前我遇到过这个错误,发现它与不包含resource_uri到相关资源有关。但在这种情况下,我发布 - 所以它不应该抱怨在嵌套资源中缺少resource_uri。不包括它们的原因是因为父资源尚未保存。 tastypie还不够先进吗?
这是追溯:
{“error_message”:“int()参数必须是字符串或数字,而不是 'list'“,”traceback“:”Traceback(最近一次调用最后一次):
File \“build / bdist.linux-x86_64 / egg / tastypie / resources.py \”,第192行, in wrapper response = callback(request,* args,** kwargs)
F> ile \“build / bdist.linux-x86_64 / egg / tastypie / resources.py \”,第397行, 在dispatch_list中返回self.dispatch('list',request,** kwargs)
File \“build / bdist.linux-x86_64 / egg / tastypie / resources.py \”,第427行, 在调度响应=方法(请求,** kwargs)
File \“build / bdist.linux-x86_64 / egg / tastypie / resources.py \”,line 1165,在post_list updated_bundle = self.obj_create(bundle, request = request,** self.remove_api_resource_names(kwargs))
File \“build / bdist.linux-x86_64 / egg / tastypie / resources.py \”,line 1783,在obj_create中m2m_bundle = self.hydrate_m2m(包)
File \“build / bdist.linux-x86_64 / egg / tastypie / resources.py \”,第743行, in hydrate_m2m bundle.data [field_name] = field_object.hydrate_m2m(束)
File \“build / bdist.linux-x86_64 / egg / tastypie / fields.py \”,第742行, hydrate_m2m m2m_hydrated.append(self.build_related_resource(value, ** kwargs))
File \“build / bdist.linux-x86_64 / egg / tastypie / fields.py \”,第593行, build_related_resource返回 self.resource_from_data(self.fk_resource,value,** kwargs)
File \“build / bdist.linux-x86_64 / egg / tastypie / fields.py \”,第548行, resource_from_data返回fk_resource.obj_update(fk_bundle,** data)
File \“build / bdist.linux-x86_64 / egg / tastypie / resources.py \”,line 1814年,在obj_update bundle.obj = self.obj_get(请求, ** lookup_kwargs)
File \“build / bdist.linux-x86_64 / egg / tastypie / resources.py \”,line 1752,在obj_get base_object_list = self.get_object_list(请求).filter(** kwargs)
文件 \ “在/ usr / local / lib目录/ python2.7 /站点包/ Django的/ DB /模型/ query.py \”, 第550行,在过滤器中返回self._filter_or_exclude(False,* args, ** kwargs)
文件 \ “在/ usr / local / lib目录/ python2.7 /站点包/ Django的/ DB /模型/ query.py \”, 第568行,在_filter_or_exclude中克隆.query.add_q(Q(* args,** kwargs))
文件 \ “在/ usr / local / lib目录/ python2.7 /站点包/ Django的/ DB /模型/ SQL / query.py \”, 第1194行,在add_q can_reuse = used_aliases,force_having = force_having)
文件 \ “在/ usr / local / lib目录/ python2.7 /站点包/ Django的/ DB /模型/ SQL / query.py \”, 第1129行,在add_filter连接器中)
文件 \ “在/ usr / local / lib目录/ python2.7 /站点包/ Django的/ DB /模型/ SQL / where.py \”, 第67行,添加值= obj.prepare(lookup_type,value)
文件 \ “在/ usr / local / lib目录/ python2.7 /站点包/ Django的/ DB /模型/ SQL / where.py \”, 第316行,在准备中返回self.field.get_prep_lookup(lookup_type, 值)
文件 \ “在/ usr / local / lib目录/ python2.7 /站点包/ Django的/ DB /模型/场/ related.py \”, 第136行,在get_prep_lookup中返回self._pk_trace(value, 'get_prep_lookup',lookup_type)
文件 \ “在/ usr / local / lib目录/ python2.7 /站点包/ Django的/ DB /模型/场/ related.py \”, 第209行,在_pk_trace v = getattr(field,prep_func)(lookup_type,v, ** kwargs)
文件 \ “在/ usr / local / lib目录/ python2.7 /站点包/ Django的/ DB /模型/场/ 的init 的.py \”, 第292行,在get_prep_lookup中返回self.get_prep_value(value)
文件 \ “在/ usr / local / lib目录/ python2.7 /站点包/ Django的/ DB /模型/场/ 的init 的.py \”, 第479行,在get_prep_value中返回int(value)
TypeError:int()参数必须是字符串或数字,而不是'list'“}
修改:
我认为这个问题是相关的:
https://github.com/toastdriven/django-tastypie/issues/307
与此测试案例一起:
https://github.com/joshbohde/django-tastypie/commit/eef8d27fb65d5777a98a3df034af26ceb317deb3