Comments (7)
@simkimsia thank you for reporting this.
I guess this problem is not related to casting, but to the fact that to keep the pointer to the input dict benedict
now stores data inside a wrapped dict -> https://github.com/fabiocaccamo/python-benedict/blob/master/benedict/dicts/base/base_dict.py
This should be a completely backward compatibility and transparent behavior.
In case of emergency, there is a new dict()
method to access the wrapped dict, so you could just do:
normal_dict = {'list': {'scope': {'pk': '109'}}
benedict_dict = benedict(normal_dict)
instance = SomeModel.objects.create(linkage=benedict_dict.dict())
retrieved = SomeModel.objects.last()
In any case this is just a workaround, and casting benedict
to dict
should work fine.
def test_cast_benedict_to_dict(self):
b = benedict({
'a': 1,
'b': {
'c': {
'd': 2,
},
},
})
d = dict(b)
self.assertTrue(isinstance(d, dict))
self.assertEqual(type(d), dict)
self.assertTrue(b == d)
self.assertFalse(b is d)
It's my intention to keep benedict 100% dict compatible, so we must understand what breaks the django JSONField
behavior to fix it.
Could you debug better the cause of this problem?
from python-benedict.
Thank you for prompt reply
I try but no promises as this is not high on priority once I have a workaround
Hope you understand
from python-benedict.
I think I found the cause.
THis is my code to replicate the error using pytest 3.4.1
import json
from benedict import benedict
"""
doc_string for test_python_class
"""
class TestBenedict:
"""
doc_string
"""
def teardown_method(self, test_method):
pass
def test_cast_benedict_to_dict(self):
"""
some doc string
"""
b = benedict({
'a': 1,
'b': {
'c': {
'd': 2,
},
},
})
d = dict(b)
assert isinstance(d, dict)
assert type(d) == dict
assert b == d
assert b is not d
def test_json_dumps_benedict(self):
"""
some doc string
"""
b = benedict({
'a': 1,
'b': {
'c': {
'd': 2,
},
},
})
dumped = json.dumps(b)
print(dumped)
assert isinstance(dumped, str)
assert dumped == '{"a": 1, "b": {"c": {"d": 2}}}'
This is the 0.20.0 effort when I run pytest test_benedict.py -s
This is the 0.19.0 effort when I run the same command and same test code
Sorry I know the best way is to create a PR and write a failing test, but I am not sure how to do that in your current setup.
So it was easier for me to write a failing test as a standalone.
Perhaps you can advise in documentation on how to write failing tests as PR.
Long story of the cause
JSONField uses an encoder for the dumping of the json input. The default encoder comes from psycopg2 which by default simply uses json.dumps
at this line https://github.com/psycopg/psycopg2/blob/e50f3129c25aa764b3acb6205f515bab56c3f6d2/lib/_json.py#L59
Hence I wrote a test to test that json.dumps works for 0.19 but fails for 0.20. The test is a success as shown above.
from python-benedict.
@simkimsia the dict()
method is used internally and if someone needs to use it, it means that there is some problem, so I intentionally omitted it from the README.
Thank you very much for the test, it's very useful!
from python-benedict.
I confirm this works ✅
from python-benedict.
@simkimsia don't worry, if you find the time, just check what is called by the JSONField
.
from python-benedict.
if you find the time, just check what is called by the JSONField.
Yes, I will try to do so.
By the way, I see that you made no commits. Yet you cite a method .dict()
as workaround I don't find in the readme.
Am I mistaken?
Update
Ok I see it in the BaseDict class https://github.com/fabiocaccamo/python-benedict/blob/master/benedict/dicts/base/base_dict.py#L51 Perhaps would be useful to update the README? I leave that to you. Looks like I chose a convoluted way to export back to regular dict in my own workaround
from python-benedict.
Related Issues (20)
- keyattr dynamic or AttributeError HOT 5
- INI support for case sensitive keys HOT 6
- `from_ini()` returns a `str` instead of a `dict` HOT 21
- Support defining how input data is loaded (filepath, url or data-string) HOT 3
- Assigning benedict element to itself clears the element HOT 2
- Minimalize dependencies and define scope HOT 1
- Move some requirements to a new `parse` extra requirement.
- Exception when comparing with Protocol classes HOT 7
- Yaml serializer produces inconsistent results HOT 5
- Add `from_html` method. HOT 2
- iterating benedict_dictionary.items(), value is not benedict_type HOT 2
- Union with assignment operator produces inconsistent results compared to `dict`. HOT 2
- README Index links don't work on PyPI, only GitHub's markdown renderer HOT 2
- Bug: Passing `data_only=True` into `from_xls()` returns excel formulas instead of cell data values HOT 5
- RecursionError while initializing from existing dict HOT 3
- Optionally allow `keypath_separator` in input dict keys (`unflatten`). HOT 6
- Benedict with Realtime Sync accross Processes HOT 7
- utility method clean does not work for nested dicts HOT 3
- add funtion SuperFlatten feature, Flatten multi-level dictionary to 1 HOT 3
- Allow keypaths to preserve order HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-benedict.