[Django] #26256: JSON serializer is not working for subset of fields.

7 views
Skip to first unread message

Django

unread,
Feb 22, 2016, 1:44:20 AM2/22/16
to django-...@googlegroups.com
#26256: JSON serializer is not working for subset of fields.
--------------------------------------+--------------------
Reporter: sonus21 | Owner: nobody
Type: Uncategorized | Status: new
Component: Core (Serialization) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
{{{
class Country(models.Model):
country = CountryField()

class City(models.Model):
name = models.CharField(max_length=1000)
country = models.ForeignKey(Country)

country = request.POST['country']
country = get_object_or_404(Country, country=country)
cities = serializers.serialize("json", country.city_set.all(),
fields=('name', 'id'))
}}}

[https://docs.djangoproject.com/en/1.8/topics/serialization/#s-subset-of-
fields]

According to above code snippet and document it should return JSON object
with only name and id field but it returns
{{{
[{"fields": {"name": "Chennai"}, "model": "job.city", "pk": 1}]
}}}
instead of
{{{
[{"name": "Chennai", "id": 1}]
}}}
which includes all fields of a model City.

--
Ticket URL: <https://code.djangoproject.com/ticket/26256>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 22, 2016, 1:44:51 AM2/22/16
to django-...@googlegroups.com
#26256: JSON serializer for subset of fields.
-------------------------------------+-------------------------------------

Reporter: sonus21 | Owner: nobody
Type: Uncategorized | Status: new
Component: Core | Version: 1.8
(Serialization) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by sonus21):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26256#comment:1>

Django

unread,
Feb 22, 2016, 4:02:37 AM2/22/16
to django-...@googlegroups.com
#26256: JSON serializer for subset of fields.
--------------------------------------+------------------------------------
Reporter: sonus21 | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by claudep):

* type: Uncategorized => Cleanup/optimization
* component: Core (Serialization) => Documentation
* stage: Unreviewed => Accepted


Comment:

The behavior is consistent. However the documentation could be a little
clearer about the structure which will be serialized for a subset of
fields. That is the basic structure with `fields`/`model`/`pk` is still
output, but the `fields` content is filtered.

--
Ticket URL: <https://code.djangoproject.com/ticket/26256#comment:2>

Django

unread,
Feb 22, 2016, 8:52:24 AM2/22/16
to django-...@googlegroups.com
#26256: JSON serializer for subset of fields.
--------------------------------------+------------------------------------
Reporter: sonus21 | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by sonus21):

Replying to [comment:2 claudep]:


> The behavior is consistent. However the documentation could be a little
clearer about the structure which will be serialized for a subset of
fields. That is the basic structure with `fields`/`model`/`pk` is still
output, but the `fields` content is filtered.

Yeah that is correct. But serializers output fields with only one value
"name" and "id" is also missing.

--
Ticket URL: <https://code.djangoproject.com/ticket/26256#comment:3>

Django

unread,
Feb 23, 2016, 2:39:40 AM2/23/16
to django-...@googlegroups.com
#26256: JSON serializer for subset of fields.
--------------------------------------+------------------------------------
Reporter: sonus21 | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by claudep):

So the question is if the `id` field when explicitly provided should be
present even when it is already given by the `pk` field at the first
level. Once again, this might be documented like "the primary key field is
always produced in the `pk` field, even if you specify it in the fields
parameter".

--
Ticket URL: <https://code.djangoproject.com/ticket/26256#comment:4>

Django

unread,
Mar 5, 2016, 2:39:28 PM3/5/16
to django-...@googlegroups.com
#26256: JSON serializer for subset of fields.
--------------------------------------+------------------------------------
Reporter: sonus21 | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by claudep):

* has_patch: 0 => 1


Comment:

A suggestion:
{{{#!diff
diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt
index 6132c63..c46dbe5 100644
--- a/docs/topics/serialization.txt
+++ b/docs/topics/serialization.txt
@@ -59,7 +59,8 @@ specify a ``fields`` argument to the serializer::
data = serializers.serialize('xml', SomeModel.objects.all(),
fields=('name','size'))

In this example, only the ``name`` and ``size`` attributes of each model
will
-be serialized.
+be serialized. The primary key is always serialized as the ``pk`` element
in the
+resulting output, it never appears in the ``fields`` part.

.. note::

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26256#comment:5>

Django

unread,
Mar 8, 2016, 9:08:55 AM3/8/16
to django-...@googlegroups.com
#26256: JSON serializer for subset of fields.
-------------------------------------+-------------------------------------
Reporter: sonus21 | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Accepted => Ready for checkin


Comment:

Makes sense to me. Since we have two independent clauses, the comma could
be a semicolon.

--
Ticket URL: <https://code.djangoproject.com/ticket/26256#comment:6>

Django

unread,
Mar 8, 2016, 3:31:07 PM3/8/16
to django-...@googlegroups.com
#26256: JSON serializer for subset of fields.
-------------------------------------+-------------------------------------
Reporter: sonus21 | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz <claude@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"c5fda55edc13118c344cbf6e862a3a6a038e5578" c5fda55e]:
{{{
#!CommitTicketReference repository=""
revision="c5fda55edc13118c344cbf6e862a3a6a038e5578"
Fixed #26256 -- Added note about primary key serialization

Thanks Sonu kumar for the report and Tim Graham for the review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26256#comment:7>

Django

unread,
Mar 8, 2016, 3:31:46 PM3/8/16
to django-...@googlegroups.com
#26256: JSON serializer for subset of fields.
-------------------------------------+-------------------------------------
Reporter: sonus21 | Owner: nobody

Type: | Status: closed
Cleanup/optimization |
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz <claude@…>):

In [changeset:"0e80ac467293c659588769ec0587a5bf5f084b49" 0e80ac4]:
{{{
#!CommitTicketReference repository=""
revision="0e80ac467293c659588769ec0587a5bf5f084b49"
[1.9.x] Fixed #26256 -- Added note about primary key serialization

Thanks Sonu kumar for the report and Tim Graham for the review.

Backport of c5fda55edc from master.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26256#comment:8>

Reply all
Reply to author
Forward
0 new messages