wlanslovenija / django-tastypie-mongoengine

MongoEngine support for django-tastypie.
Other
73 stars 59 forks source link

Problem with PUT and _id #80

Open Seraf opened 10 years ago

Seraf commented 10 years ago

Hello,

I don't know if the issue is related to mongoengine or tastypie, but I have a problem with a PUT.

I use angularjs and RESTangular to create this PUT, but I reproduced by writing my PUT request manually.

When I try to save my user (https://github.com/Seraf/LISA/blob/angularjs/lisa/server/web/weblisa/api/accounts.py#L22), it throw me a :

{"error_message": "Could not save document (Mod on _id not allowed)", "traceback": "Traceback (most recent call last):
File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/tastypie/resources.py\", line 201, in wrapper
response = callback(request, *args, **kwargs)

File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/tastypie/resources.py\", line 441, in dispatch_detail
return self.dispatch('detail', request, **kwargs)

File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 390, in dispatch
return self._wrap_request(request, lambda: super(MongoEngineResource, self).dispatch(request_type, request, **kwargs))

File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 360, in _wrap_request
return fun()

File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 390, in <lambda>
return self._wrap_request(request, lambda: super(MongoEngineResource, self).dispatch(request_type, request, **kwargs))

File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/tastypie/resources.py\", line 464, in dispatch\n    response = method(request, **kwargs)\n\n  File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/tastypie/resources.py\", line 1431, in put_detail\n    updated_bundle = self.obj_update(bundle=bundle, **self.remove_api_resource_names(kwargs))

File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 565, in obj_update
return self.save(bundle, skip_errors=skip_errors)

File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 582, in save
return super(MongoEngineResource, self).save(bundle, skip_errors)

File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2253, in save\n    bundle.obj.save()

File \"/home/seraf/Sandbox/LISA/lisa/server/web/interface/models.py\", line 29, in save
return super(LisaUser, self).save(*args, **kwargs)

File \"/home/seraf/.virtualenvs/LISA/local/lib/python2.7/site-packages/mongoengine/document.py\", line 296, in save
raise OperationError(message % unicode(err))

OperationError: Could not save document (Mod on _id not allowed)\n"}

My user is a custom class : https://github.com/Seraf/LISA/blob/angularjs/lisa/server/web/interface/models.py#L15

So, digging a little bit to understand why it doesn't work, I tried with my plugin resource : https://github.com/Seraf/LISA/blob/angularjs/lisa/server/web/manageplugins/api.py#L14

And it worked.

So I tried to see why it detected an _id field as changed on my user resource, and not my plugin.

By adding some debug in : Mongoengine/document.py (line 242)

object_id = doc['_id']
updates, removals = self._delta()
print "====UPDATES/REMOVALS===="
print updates
print removals
# Need to add shard key to query, or you get an error
select_dict = {'_id': object_id}

Here is the ouput with a PUT of my user User :

2014-08-21 09:51:50+0200 [HTTPChannel,52,127.0.0.1] ====UPDATES/REMOVALS====
2014-08-21 09:51:50+0200 [HTTPChannel,52,127.0.0.1] {'api_key_created': datetime.datetime(2014, 8, 18, 9, 31, 43, 944000, tzinfo=<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>), '_id': ObjectId('53f1abdfa9a7bf5358f3d046'), 'last_login': datetime.datetime(2014, 8, 21, 7, 35, 30, 736000, tzinfo=<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>), 'date_joined': datetime.datetime(2014, 8, 18, 7, 31, 43, 898000, tzinfo=<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>)}
2014-08-21 09:51:50+0200 [HTTPChannel,52,127.0.0.1] {}
2014-08-21 09:51:50+0200 [-] "127.0.0.1" - - [21/Aug/2014:07:51:50 +0000] "PUT /backend/api/v1/user/53f1abdfa9a7bf5358f3d046/?format=json HTTP/1.1" 500 2467 "http://lisa:8000/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/36.0.1985.125 Chrome/36.0.1985.125 Safari/537.36

Here is a put with my plugin : Plugin :

2014-08-21 11:03:38+0200 [HTTPChannel,83,127.0.0.1] ====UPDATES/REMOVALS====
2014-08-21 11:03:38+0200 [HTTPChannel,83,127.0.0.1] {'description.0.description': u'Ce plugin est celui par d\xe9faut int\xe9grant les interactions basiques avec LISA', 'description.1.lang': u'en', 'description.0.lang': u'fr', 'description.1.description': u'This plugin is shipped by default, managing all basic interactions with LISA'}
2014-08-21 11:03:38+0200 [HTTPChannel,83,127.0.0.1] {'id': 1}
2014-08-21 11:03:38+0200 [-] "127.0.0.1" - - [21/Aug/2014:09:03:38 +0000] "PUT /backend/api/v1/plugin/53dba55aa9a7bf3c9da33373/?format=json HTTP/1.1" 204 - "http://lisa:8000/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/36.0.1985.125 Chrome/36.0.1985.125 Safari/537.36"

Both sent the id field the same way, and I received the request.body.id identical ... So I don't understand the difference between them ... It appears there's a problem with the hydrate (or dehydrate) functions, but don't know why ...

If you have an idea ...

Thanks a lot !

Seraf commented 9 years ago

Hi, any new about it please ?