milesrichardson / ParsePy

A relatively up-to-date fork of ParsePy, the Python wrapper for the Parse.com API. Originally maintained by @dgrtwo
MIT License
516 stars 184 forks source link

select_related needs deserialization support #126

Closed Greyvend closed 9 years ago

Greyvend commented 9 years ago

Here is the snippet that demonstrates the problem.

from parse_rest.connection import register
from parse_rest.datatypes import Object as ParseObject

APPLICATION_ID = "APPLICATION_ID"  # needs to be valid
REST_API_KEY = "REST_API_KEY"  # needs to be valid

register(APPLICATION_ID, REST_API_KEY)

class MainObject(ParseObject):
    pass

class RelatedObject(ParseObject):
    pass

obj1 = RelatedObject(field1='val1', field2='val2')
obj1.save()
obj2 = RelatedObject(field1='val3', field2='val4')
obj2.save()

array_of_related_objects = [obj1, obj2]

main = MainObject(field1='val1_1', related_objects=array_of_related_objects)
main.save()

main_objects = MainObject.Query.all().select_related('related_objects')
assert hasattr(main_objects[0].related_objects[0], 'field1')  # it fails! The type of related_objects is dict, not an Object.

So when using select_related method the specified attribute doesn't seem to be transformed from raw representation to Object.

flplv commented 9 years ago

The problem you are facing is not with select_related, instead, it is that current version of ParsePy does not deserialize arrays of objects.

I have proposed a pull request to fix the deserialization here: https://github.com/dgrtwo/ParsePy/pull/121, can you please test if the patch will fix your problem?

Thanks!

flplv commented 9 years ago

Also, the correct assertion is: assert hasattr(main_objects[0].related_objects[0], 'field1'), since related_objects field is an Array in parse, and a list in Python.

Greyvend commented 9 years ago

Yes, your change fixes it. Fortunately, @dgrtwo already merged it.

flplv commented 9 years ago

Perfect. Glad to help.

On Wed, Sep 30, 2015 at 2:36 AM, Serge Mosin notifications@github.com wrote:

Yes, your change fixes it. Fortunately, @dgrtwo https://github.com/dgrtwo already merged it.

— Reply to this email directly or view it on GitHub https://github.com/dgrtwo/ParsePy/issues/126#issuecomment-144286846.

Skype: felipeanl