#26256 closed Cleanup/optimization (fixed)
JSON serializer for subset of fields.
Reported by: | Sonu kumar | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.8 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Change History (8)
comment:1 by , 9 years ago
Summary: | JSON serializer is not working for subset of fields. → JSON serializer for subset of fields. |
---|
follow-up: 3 comment:2 by , 9 years ago
Component: | Core (Serialization) → Documentation |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Cleanup/optimization |
comment:3 by , 9 years ago
Replying to 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 thefields
content is filtered.
Yeah that is correct. But serializers output fields with only one value "name" and "id" is also missing.
comment:4 by , 9 years ago
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".
comment:5 by , 9 years ago
Has patch: | set |
---|
A suggestion:
-
docs/topics/serialization.txt
diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt index 6132c63..c46dbe5 100644
a b specify a ``fields`` argument to the serializer:: 59 59 data = serializers.serialize('xml', SomeModel.objects.all(), fields=('name','size')) 60 60 61 61 In this example, only the ``name`` and ``size`` attributes of each model will 62 be serialized. 62 be serialized. The primary key is always serialized as the ``pk`` element in the 63 resulting output, it never appears in the ``fields`` part. 63 64 64 65 .. note:: 65 66
comment:6 by , 9 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Makes sense to me. Since we have two independent clauses, the comma could be a semicolon.
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 thefields
content is filtered.