Opened 15 years ago

Closed 14 years ago

#11244 closed (wontfix)

serialization only works for iterable objects

Reported by: Andrea Zilio Owned by: nobody
Component: Core (Serialization) Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This works:

from django.core import serializers
data = serializers.serialize("json", SomeModel.objects.all())

This doesn't and throws TypeError:'SomeModel' object is not iterable:

from django.core import serializers
data = serializers.serialize("json", SomeModel.objects.get(pk=1))

I think that should be possible to serialize single objects too.

(Es: I often need to serialize a object, or better... some fields of a single object when implementing ajax views)

Change History (4)

comment:1 by Karen Tracey, 15 years ago

Resolution: wontfix
Status: newclosed

It's trivially easy do this in your calling code:

from django.core import serializers
data = serializers.serialize("json", [SomeModel.objects.get(pk=1)])

I don't see why the code/doc for serializers.serialize should be made more complicated just to handle this.

in reply to:  1 comment:2 by Andrea Zilio, 15 years ago

Replying to kmtracey:

It's trivially easy do this in your calling code:

from django.core import serializers
data = serializers.serialize("json", [SomeModel.objects.get(pk=1)])

I don't see why the code/doc for serializers.serialize should be made more complicated just to handle this.

Because the resulting json string does not represent an object but an array of one single object and I don't see why the code/doc of the client side javascript should be made more complicated just to handle an array when expecting an object ;)

comment:3 by Oroku Saki, 14 years ago

Resolution: wontfix
Status: closedreopened
Triage Stage: UnreviewedDesign decision needed

@kmtracy I might be overstepping my bounds (ie, feel free to slap me in the face and re-close this ticket if I am), but I strongly agree with epper. It's easy to use one object in a list but then you have to either iterate through the object (with only one iteration) or use your JavaScript to pull out the one object like my_object[0] over and over again (DRY comes to mind). I would suggest that if an object (instead of a QuerySet) is passed into the method that the output reflect this as well. It just seems right. When it comes to an API (JSON in my case) it's all about clean code and clean output.

comment:4 by Karen Tracey, 14 years ago

Resolution: wontfix
Status: reopenedclosed

Please don't reopen wontfixed issues without first gaining some consensus for it on the development list. See: http://docs.djangoproject.com/en/dev/internals/contributing/#id1.

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