Code

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#3466 closed (fixed)

'fields' and 'stream' options not working for json serializer

Reported by: Parand Darugar <tdarugar@…> Owned by: jacob
Component: Core (Serialization) Version: master
Severity: Keywords:
Cc: egmanoj@…, ferringb@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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)

tests.py (1.8 KB) - added by Simon G. <dev@…> 7 years ago.
core.serializers.json.diff (456 bytes) - added by egmanoj@… 7 years ago.
Patch for core.serializers.json.Serializer
modeltests.serializers.models.diff (1.0 KB) - added by egmanoj@… 7 years ago.
Added new tests to tests.modeltests.serializers.models
core.serializers.json.2.diff (676 bytes) - added by SmileyChris 7 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 7 years ago by Simon G. <dev@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

I've confirmed this bug with the latest svn, just working on some tests for it.

comment:2 Changed 7 years ago by Simon G. <dev@…>

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.

Changed 7 years ago by Simon G. <dev@…>

comment:3 Changed 7 years ago by egmanoj@…

  • 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.

Changed 7 years ago by egmanoj@…

Patch for core.serializers.json.Serializer

Changed 7 years ago by egmanoj@…

Added new tests to tests.modeltests.serializers.models

comment:4 Changed 7 years ago by egmanoj@…>

  • Cc egmanoj@… added

comment:5 Changed 7 years ago by SmileyChris

  • Summary changed from 'fields' option not working for json serializer to 'fields' and 'stream' options not working for json serializer

This is an issue for stream too.

Changed 7 years ago by SmileyChris

comment:6 Changed 7 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(In [5409]) Fixed #3466 -- Fixed problem with specifyin a 'fields' argument to a JSON serializer. Also added documenation for the 'fields' argument.

comment:7 Changed 7 years ago by (removed)

  • Cc ferringb@… added
  • Resolution fixed deleted
  • Status changed from closed to 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'


----------------------------------------------------------------------

comment:8 follow-up: Changed 7 years ago by russellm

  • Resolution set to fixed
  • Status changed from reopened to 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 in reply to: ↑ 8 Changed 7 years ago by (removed)

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 :/

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.