#3466 closed (fixed)
'fields' and 'stream' options not working for json serializer
Reported by: | Owned by: | Jacob | |
---|---|---|---|
Component: | Core (Serialization) | Version: | dev |
Severity: | Keywords: | ||
Cc: | egmanoj@…, ferringb@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'm attempting to use the 'fields' option with the json serializer without any luck. The same call with the xml serializer works fine:
serializers.serialize("json", (Something.objects.get(pk=1), ), fields='myfield' )
breaks with:
Traceback (most recent call last):
File "<console>", line 1, in ?
File "C:\Python24\lib\site-packages\django\core\serializers\init.py", line 55, in serialize s.serialize(queryset, options)
File "C:\Python24\lib\site-packages\django\core\serializers\base.py", line 49, in serialize self.end_serialization()
File "C:\Python24\lib\site-packages\django\core\serializers\json.py", line 19, in end_serialization
simplejson.dump(self.objects, self.stream, cls=DateTimeAwareJSONEncoder, self.options)
File "C:\Python24\lib\site-packages\django\utils\simplejson\init.py", line 134, in dump check_circular=check_circular, allow_nan=allow_nan, indent=indent,
TypeError: init() got an unexpected keyword argument 'fields'
Trying:
serializers.serialize("xml", (Something.objects.get(pk=1), ), fields='myfield' )
works fine however.
Attachments (4)
Change History (13)
comment:1 by , 18 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 18 years ago
I've attached tests which show this problem in action. It's just the json serializer that's affected. Unfortunately, I'm not entirely sure what to do to fix it.
by , 18 years ago
comment:3 by , 18 years ago
Has patch: | set |
---|
I have added a couple of tests and a patch.
The patch affects the end_serialization method of core.serializers.json.Serializer. The rationale for removing fields from self.options is that by the time this method gets called, fields would already have served its purpose.
by , 18 years ago
Attachment: | core.serializers.json.diff added |
---|
Patch for core.serializers.json.Serializer
by , 18 years ago
Attachment: | modeltests.serializers.models.diff added |
---|
Added new tests to tests.modeltests.serializers.models
comment:4 by , 18 years ago
Cc: | added |
---|
comment:5 by , 18 years ago
Summary: | 'fields' option not working for json serializer → 'fields' and 'stream' options not working for json serializer |
---|
This is an issue for stream too.
by , 18 years ago
Attachment: | core.serializers.json.2.diff added |
---|
comment:6 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:7 by , 17 years ago
Cc: | added |
---|---|
Resolution: | fixed |
Status: | closed → reopened |
Reopening (proper route, or...?) since rev 3331 (this patch) fails tests, upon commiting horked the build.
====================================================================== ERROR: test_json_serializer_fields (regressiontests.serializers_regress.tests.SerializerTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/django/utils/functional.py", line 3, in _curried return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs)) File "/home/bharring/archives/django/svn-django/tests/regressiontests/serializers_regress/tests.py", line 290, in fieldsTest serialized_data = serializers.serialize(format, [obj], indent=2, fields=('field1','field3')) File "/usr/lib/python2.4/site-packages/django/core/serializers/__init__.py", line 67, in serialize s.serialize(queryset, **options) File "/usr/lib/python2.4/site-packages/django/core/serializers/base.py", line 49, in serialize self.end_serialization() File "/usr/lib/python2.4/site-packages/django/core/serializers/json.py", line 24, in end_serialization simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options) File "/usr/lib/python2.4/site-packages/django/utils/simplejson/__init__.py", line 134, in dump check_circular=check_circular, allow_nan=allow_nan, indent=indent, TypeError: __init__() got an unexpected keyword argument 'fields' ====================================================================== ERROR: test_json_serializer_stream (regressiontests.serializers_regress.tests.SerializerTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/django/utils/functional.py", line 3, in _curried return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs)) File "/home/bharring/archives/django/svn-django/tests/regressiontests/serializers_regress/tests.py", line 290, in fieldsTest serialized_data = serializers.serialize(format, [obj], indent=2, fields=('field1','field3')) File "/usr/lib/python2.4/site-packages/django/core/serializers/__init__.py", line 67, in serialize s.serialize(queryset, **options) File "/usr/lib/python2.4/site-packages/django/core/serializers/base.py", line 49, in serialize self.end_serialization() File "/usr/lib/python2.4/site-packages/django/core/serializers/json.py", line 24, in end_serialization simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options) File "/usr/lib/python2.4/site-packages/django/utils/simplejson/__init__.py", line 134, in dump check_circular=check_circular, allow_nan=allow_nan, indent=indent, TypeError: __init__() got an unexpected keyword argument 'fields' ====================================================================== FAIL: Doctest: modeltests.serializers.models.__test__.API_TESTS ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/django/test/_doctest.py", line 2161, in runTest raise self.failureException(self.format_failure(new.getvalue())) AssertionError: Failed doctest test for modeltests.serializers.models.__test__.API_TESTS File "/home/bharring/archives/django/svn-django/tests/modeltests/serializers/models.py", line unknown line number, in API_TESTS ---------------------------------------------------------------------- File "/home/bharring/archives/django/svn-django/tests/modeltests/serializers/models.py", line ?, in modeltests.serializers.models.__test__.API_TESTS Failed example: print serializers.serialize("json", Article.objects.all(), fields=('headline','pub_date')) Exception raised: Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/django/test/_doctest.py", line 1248, in __run compileflags, 1) in test.globs File "<doctest modeltests.serializers.models.__test__.API_TESTS[49]>", line 1, in ? print serializers.serialize("json", Article.objects.all(), fields=('headline','pub_date')) File "/usr/lib/python2.4/site-packages/django/core/serializers/__init__.py", line 67, in serialize s.serialize(queryset, **options) File "/usr/lib/python2.4/site-packages/django/core/serializers/base.py", line 49, in serialize self.end_serialization() File "/usr/lib/python2.4/site-packages/django/core/serializers/json.py", line 24, in end_serialization simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options) File "/usr/lib/python2.4/site-packages/django/utils/simplejson/__init__.py", line 134, in dump check_circular=check_circular, allow_nan=allow_nan, indent=indent, TypeError: __init__() got an unexpected keyword argument 'fields' ----------------------------------------------------------------------
follow-up: 9 comment:8 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
I suspect PEBKAC :-)
Your error trace indicates that the call to simplejson.dump is on line 24 of serializers/json.py. A current trunk puts that statement on line 26.
Either you haven't updated to [5409], or you have a stale Django version floating around somewhere.
I have no idea what you're referring to when you talk about "Rev 3331 (this patch)" - the patch was revision [5409], on ticket #3466. Previous revision for the file was [5311].
comment:9 by , 17 years ago
Replying to russellm:
I suspect PEBKAC :-)
Guess my mother was right when she said I was 'special'... :/
Your error trace indicates that the call to simplejson.dump is on line 24 of serializers/json.py. A current trunk puts that statement on line 26.
Either you haven't updated to [5409], or you have a stale Django version floating around somewhere.
I have no idea what you're referring to when you talk about "Rev 3331 (this patch)" - the patch was revision [5409], on ticket #3466. Previous revision for the file was [5311].
Two sets of failures there; stale version bleeding through for tests (site-packages), and listed the bzr proxy rev instead of the svn rev (use bzr+svn so I can do my own development locally).
So yeah, PEBKAC, I suck, pardon for the noise :/
I've confirmed this bug with the latest svn, just working on some tests for it.