Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#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 Sonu kumar, 9 years ago

Summary: JSON serializer is not working for subset of fields.JSON serializer for subset of fields.

comment:2 by Claude Paroz, 9 years ago

Component: Core (Serialization)Documentation
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

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.

in reply to:  2 comment:3 by Sonu kumar, 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 the fields content is filtered.

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

Version 0, edited 9 years ago by Sonu kumar (next)

comment:4 by Claude Paroz, 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 Claude Paroz, 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::  
    5959    data = serializers.serialize('xml', SomeModel.objects.all(), fields=('name','size'))
    6060
    6161In this example, only the ``name`` and ``size`` attributes of each model will
    62 be serialized.
     62be serialized. The primary key is always serialized as the ``pk`` element in the
     63resulting output, it never appears in the ``fields`` part.
    6364
    6465.. note::
    6566

comment:6 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin

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

comment:7 by Claude Paroz <claude@…>, 9 years ago

Resolution: fixed
Status: newclosed

In c5fda55e:

Fixed #26256 -- Added note about primary key serialization

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

comment:8 by Claude Paroz <claude@…>, 9 years ago

In 0e80ac4:

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

Note: See TracTickets for help on using tickets.
Back to Top