-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fields will not read what it writes #60
Conversation
When to_representation is run on a geo object json.load adds unicode strings so the result is something like: ``` {u'type': u'Point', u'coordinates': [39.921092168090915, 28.04062843322754]} ``` this is the string that rest_framework puts into the web form for any PUTs to edit the field. Therefore the PUT will send the string: ``` "{u'type': u'Point', u'coordinates': [39.921092168090915, 28.04062843322754]}" ``` which will not work with GEOSGeometry as it does not expect the u symbols. The way around this is to eval the string as a python dictionary and then dump the json. This creates the string: ``` '{"type": "Point", "coordinates": [39.921092168090915, 28.04062843322754]}' ``` which will then work.
The other maybe cleaner option is to create a JSONDict class that overwrites the str for dict to use json.dumps, i.e.
Then returning: JSONDict(json.loads(GEOSGeometry(value).geojson)) from to_representation makes the string:
|
Return dictionary with correct json __str__ value
Only update the __str__ of the returned dict
1 similar comment
If the solution was ok, it would have been simpler to just return Unfortunately this affects the JSON response too, in which geojson objects become strings. This is a known issue unfortunately. |
well actually I was wrong, your solution works. |
@nparley could you squash your 4 commits into one commit with a meaningful commit message please? |
Valid for python 2 only. Avoid representations like {u'type': u'Point', u'coordinates': [39.921092168090915, 28.04062843322754]} For more information see #60
* reintroduced disabled test * avoid double rendering of geojson string * added TODO hint for removing this code when python 2 will be deprecated
When to_representation is run on a geo object json.load adds unicode strings so the result is something like:
this is the string that rest_framework puts into the web form for any PUTs to edit the field. Therefore the PUT will send the string:
which will not work with GEOSGeometry as it does not expect the u symbols. The way around this is to eval the string as a python dictionary and then dump the json. This creates the string:
which will then work.