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.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/26256#comment:1>
* 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>
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>
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>
* 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>
* 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>
* 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>
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>