Search code examples
pythondjangotastypiemongoengine

django-tastypie and django-tastypie-mongoengine PATCH error


Trying to submit a PATCH request via python requests as well as with command-line cURL and I'm getting the following response:

>>> r = requests.patch(url)
>>> r.text
u'{"error_message": "PATCH",
   "traceback": "Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tastypie/resources.py", line 201, in wrapper
    response = callback(request, *args, **kwargs)
File "/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tastypie/resources.py", line 441, in dispatch_detail
    return self.dispatch('detail', request, **kwargs)
File "/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_tastypie_mongoengine-0.4.5-py2.7.egg/tastypie_mongoengine/resources.py", line 385, in dispatch
    assert request.method.lower() not in ('put', 'post', 'patch'), request.method
AssertionError: PATCH"}'
>>> r.request.method.lower()
'patch'
>>> 

Not sure if I'm missing something, but it looks like the PATCH method is in the list and the assert shouldn't trigger. It's not even making it far enough to validate that 'patch' is an allowed method for the Resource, which it is:

allowed_methods = ('get', 'post', 'patch')

UPDATE: POST is causing the same problem! This code worked fine for POST a week or two ago and no changes were made to it. The only thing that's been changed recently (OSX laptop) is the latest round of OS patches. I also noticed this github Issue that I've replied to: https://github.com/wlanslovenija/django-tastypie-mongoengine/issues/91. Not sure if that's the same problem (POST should allow a request body so I don't know).


Solution

  • Turns out the main issue was that POST and PATCH both required some of the data to be passed in the body while others needed to be passed in the URL parameters.