Django Tastypie PATCH throws "'Bundle' object is not iterable" error
api, django, patch, rest, tastypie
Solution
Sounds like your validation is trying to iterate through your foreign key bundles with this line:
for one_uri in uris:
That's where the `"Bundle" object is not iterable` is coming from. If you want to iterate over those fields as resource_uris, remove `full=True` from those FK fields.
If you want to keep them as `full=True`, you'll need to update your validation to either handle bundles for those fields or exclude them from the validation by using `exclude` in your form Meta class:
class ModelFormValidation:
...
class Meta:
exclude = (
authors,
posts,
cover_photo
)
Problem
I'm working on an API and for one of my endpoints I want to be able to make 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'm using POSTMAN to make a PATCH request to update a field in the Story model. It returns with this error: {"error_message": "'Bundle' object is not iterable", "traceback": "Traceback (most recent call last):\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 192, in wrapper\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 dispatch\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, deserialized)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", 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 is_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: 'Bundle' object is not iterable\n"} I'm not really sure whats wrong and I can't seem to find this bug anywhere else.