Django Tastypie PATCH throws the "Bundle" object is not iterable "

I am working on an API and for one of my endpoints I want to be able to do partial updates.

Here is the tastypie resource

class StoryResource(ModelResource): authors = fields.ToManyField(SimpleAuthorResource, 'authors', full=True) posts = fields.ToManyField(SimplePostResource, 'posts', full=True, blank=True) cover_photo = fields.ForeignKey(PhotoResource, 'cover_photo', full=True) class Meta: queryset = Story.objects.all() resource_name = 'story' validation = ModelFormValidation(form_class=StoryForm) authorization = Authorization() allowed_methods = ['get', 'post', 'patch', 'put'] ordering = ['-created_ts'] def determine_format(self, request): return "application/json" 

And I use POSTMAN to make a PATCH request to update a field in the Story model.

It returns with this error:

{"error_message": "The Bundle object is not iterable", "traceback": "Traceback (last last call): \ n \ n File \" /usr/local/lib/python2.7/dist -packages / tastypie / resources.py \ ", line 192, in the shell \ n response = callback (request, * args, ** kwargs) \ n \ n File \" /usr/local/lib/python2.7/dist -packages / tastypie /resources.py \ ", line 406, in dispatch_detail \ n return self.dispatch ('detail', request, ** kwargs) \ n \ n File \" /usr/local/lib/python2.7/dist-packages /tastypie/resources.py \ ", line 427, in the manager \ n response = method (request, ** kwargs) \ n \ n File \" /usr/local/lib/python2.7/dist- packages / tastypie / resources.py \ ", line 1332, in patch_detail \ n self.update_in_place (request, bundle, deserialization) \ n \ n File \" /usr/local/lib/python2.7/dist-packages/tastypie/resources.p y \ ", line 1345, in update_in_place \ n self.is_valid (original_bundle, request) \ n \ n File \" /usr/local/lib/python2.7/dist-packages/tastypie/resources.py \ ", line 991, in_valid \ n errors = self._meta.validation.is _valid (bundle, request) \ n \ n File \ "/var/www/novella-django/novella/novella/api/validation.py \", line 55 , in is_valid \ n data [field] = self.uri_to_pk (data [field]) \ n \ n File \ "/var/www/novella-django/novella/novella/api/validation.py \", line 29, in uri_to_pk \ n for one_uri in uris: \ n \ nTypeError: The "Bundle" object is not iterable \ n "}

I'm not quite sure what happened, and I cannot find this error anywhere else.

+4
source share
1 answer

It looks like your check is trying to iterate over your foreign key packages with this line:

 for one_uri in uris: 

Where the "Bundle" object is not iterable comes from "Bundle" object is not iterable . If you want to iterate over these fields as resource_uris, remove full=True from these FK fields.


If you want to save them as full=True , you need to update your validation in order to process packages for these fields or exclude them from the validation using exclude in your form. Meta class:

 class ModelFormValidation: ... class Meta: exclude = ( authors, posts, cover_photo ) 
+4
source

Source: https://habr.com/ru/post/1444961/


All Articles