adsy
adsy

Reputation: 11417

Django-tastypie does not like POST with nested resources (full=True)

Im using backbone-tastypie to create a nested resource as so.

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'

For getting the ServiceResource it works great. I get back a Service object back which contains all the ServiceSpecification's which in turn has all the ServiceSpecRange's/constants etc.

Putting the whole nested resource back to the server also works fine for existing data.

However, whenever I try to post a whole nested resource in one go (new service with new servicespec attached) I get:

int() argument must be a string or a number, not 'list'

I have had this error before, and found it related to not including a resource_uri to a related resource. But in this case, i'm posting - so it shouldn't be complaining about missing resource_uri's in a nested resource. The reason they aren't included is because the parent resource hasn't been saved yet. Is tastypie not advanced enough to see this?

Here is the traceback:

{ "error_message":"int() argument must be a string or a number, not 'list'", "traceback":"Traceback (most recent call last):

File \"build/bdist.linux-x86_64/egg/tastypie/resources.py\", line 192, in wrapper response = callback(request, *args, **kwargs)

F> ile \"build/bdist.linux-x86_64/egg/tastypie/resources.py\", line 397, in dispatch_list return self.dispatch('list', request, **kwargs)

File \"build/bdist.linux-x86_64/egg/tastypie/resources.py\", line 427, in dispatch response = method(request, **kwargs)

File \"build/bdist.linux-x86_64/egg/tastypie/resources.py\", line 1165, in 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, in obj_create m2m_bundle = self.hydrate_m2m(bundle)

File \"build/bdist.linux-x86_64/egg/tastypie/resources.py\", line 743, in hydrate_m2m bundle.data[field_name] = field_object.hydrate_m2m(bundle)

File \"build/bdist.linux-x86_64/egg/tastypie/fields.py\", line 742, in hydrate_m2m m2m_hydrated.append(self.build_related_resource(value, **kwargs))

File \"build/bdist.linux-x86_64/egg/tastypie/fields.py\", line 593, in build_related_resource return self.resource_from_data(self.fk_resource, value, **kwargs)

File \"build/bdist.linux-x86_64/egg/tastypie/fields.py\", line 548, in resource_from_data return fk_resource.obj_update(fk_bundle, **data)

File \"build/bdist.linux-x86_64/egg/tastypie/resources.py\", line 1814, in obj_update bundle.obj = self.obj_get(request, **lookup_kwargs)

File \"build/bdist.linux-x86_64/egg/tastypie/resources.py\", line 1752, in obj_get base_object_list = self.get_object_list(request).filter(**kwargs)

File \"/usr/local/lib/python2.7/site-packages/django/db/models/query.py\", line 550, in filter return self._filter_or_exclude(False, *args, **kwargs)

File \"/usr/local/lib/python2.7/site-packages/django/db/models/query.py\", line 568, in _filter_or_exclude clone.query.add_q(Q(*args, **kwargs))

File \"/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py\", line 1194, in add_q can_reuse=used_aliases, force_having=force_having)

File \"/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py\", line 1129, in add_filter connector)

File \"/usr/local/lib/python2.7/site-packages/django/db/models/sql/where.py\", line 67, in add value = obj.prepare(lookup_type, value)

File \"/usr/local/lib/python2.7/site-packages/django/db/models/sql/where.py\", line 316, in prepare return self.field.get_prep_lookup(lookup_type, value)

File \"/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py\", line 136, in get_prep_lookup return self._pk_trace(value, 'get_prep_lookup', lookup_type)

File \"/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py\", line 209, in _pk_trace v = getattr(field, prep_func)(lookup_type, v, **kwargs)

File \"/usr/local/lib/python2.7/site-packages/django/db/models/fields/init.py\", line 292, in get_prep_lookup return self.get_prep_value(value)

File \"/usr/local/lib/python2.7/site-packages/django/db/models/fields/init.py\", line 479, in get_prep_value return int(value)

TypeError: int() argument must be a string or a number, not 'list' " }

EDIT:

I believe this issue is related:

https://github.com/toastdriven/django-tastypie/issues/307

Along with this test case:

https://github.com/joshbohde/django-tastypie/commit/eef8d27fb65d5777a98a3df034af26ceb317deb3

Upvotes: 2

Views: 2839

Answers (1)

adsy
adsy

Reputation: 11417

This has been fixed in another branch. See the pull request.

Upvotes: 1

Related Questions