wlanslovenija / django-tastypie-mongoengine

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

Problem updating resource containing EmbeddedListField of Resource containing ReferenceField (and other fields) #35

Closed m-vdb closed 11 years ago

m-vdb commented 11 years ago

Hi !

I've got this schema :

class PipeResource(resources.MongoEngineResource):
    exporters = EmbeddedListField(of='api.resources.EmbeddedExporterListResource',
                          attribute='exporters', full=True, null=True)

class EmbeddedExporterListResource(resources.MongoEngineResource):
    exporter = ReferenceField(to='api.resources.ExporterResource',
                        attribute='exporter', full=True)

And when trying to update a PipeResource, I've got this error :

File "/lib/python2.7/site-packages/mongoengine/queryset.py", line 639, in _lookup_field % field_name)
InvalidQueryError: Cannot resolve field "resource_uri"

So am I doing things right or do I miss something ? Because it seems that Tastypie hands the resource_uri field to MongoEngine, and it shouldn't... I precise that the EmbeddedExporterListResource is not only a ReferenceField, but contains other fields

Thansks for your help, I post this on stackoverflow too : http://stackoverflow.com/questions/13287098

mitar commented 11 years ago

Please create a pull request with failing test.

m-vdb commented 11 years ago

yep, trying to find where it bugs with my best friend pdb, and if i succeed you'll have a pull request with a fix :D

mitar commented 11 years ago

No no. You can also just make a pull request with a test which fails. And then I can also look.

m-vdb commented 11 years ago

ok as you want, but since I need this to be fixed asap, I'll investigate too

mitar commented 11 years ago

OK. So yes. You can create pull request with test. And then later on augment it with another commit with a fix. :-)

m-vdb commented 11 years ago

done :)

btw, is there any support for DateTimeField ? (I can't see any document with a mongoengine.DateTimeField in documents.py)

mitar commented 11 years ago

Yes, that one should be automatically available in resource.

m-vdb commented 11 years ago

okay, I have something that fails, so I'll do another pull request.

For the first problem, I identified the cause :

in File tastypie_mongoengine.resources.py, line 475:

def obj_get(self, request=None, **kwargs):
    # MongoEngine exceptions are separate from Django exceptions, we combine them here
    try:
        return super(MongoEngineResource, self).obj_get(request, **kwargs)

In the returned function, tastypie does a query on the documents, and uses filter(**kwargs) on the queryset. And kwargs contains resource_uri, so that fails. A quick fix for me would be inserting kwargs.pop('resource_uri', None), but maybe this can be inserted upstream.

mitar commented 11 years ago

You are doing invalid REST request.

m-vdb commented 11 years ago

hum why is that ? could you explain a little more ?

mitar commented 11 years ago

See the test I fixed.

m-vdb commented 11 years ago

ok thanks for your help. Btw, my request was not that invalid. If I remove 'put' from the ExporterResource.allowed_method, the problem does not occur and everything goes well. Anyway, thanks for your help and your plugin :+1:

mitar commented 11 years ago

That's semi-normal. :-)

To be user friendly, you can specify referenced document by any unique combination of fields (like id). But on the other hand you can also in-place create a referenced document by specifying values. And then there is another user friendly thing: if there is some additional field in the request which is not in the document, it is ignored. Sometimes it can be not so intuitive how this interacts. :-) resource_uri is not a real field so when it tries to do a query it fails with error above. If you would remove resource_uri but leave other two fields, it should work. On the other hand, if it tries to create a new referenced document, it ignores resource_uri and just creates a new embedded document. So I am not so sure "problem does not occur" really happens. Probably it creates another referenced document, it does not reference the one you want.

This happens when you want to be user friendly and resolve ambiguities. :-)

m-vdb commented 11 years ago

wow thanks for the explanation. I'll check that it does not create another document and that it does exactly what I want :-)

mitar commented 11 years ago

OK, maybe that auto-creation does not really work always. :-)